leecode 206. 反转链表
作者:互联网
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
class Solution {
public ListNode reverseList(ListNode head) {
if(head == null || head.next == null){
return head;
}
ListNode pre = new ListNode (-1);
ListNode notIn = head;
while(notIn != null) {
ListNode tail = pre.next;
ListNode temp = notIn.next;
ListNode in = notIn;
in.next = tail;
pre.next = in;
notIn = temp;
}
return pre.next;
}
}
标签:pre,head,notIn,ListNode,206,next,链表,leecode,null 来源: https://blog.csdn.net/haponchang/article/details/98726880