/*
新建一个pre head node记录开头
*/
class Solution {
public ListNode reverseList(ListNode head) {
if(head == null || head.next == null) return head;
ListNode newhead = new ListNode(0);
newhead.next = head;
ListNode pre = newhead;
ListNode cur = head;
while(cur.next != null){
ListNode next = cur.next;
cur.next = next.next;
next.next = pre.next;
pre.next =next;
}
return newhead.next;
}
}
/*
1->2->3->4->null
直接指向null
2 ->3->4-null
cur
1 ->null
pre
*/
class Solution {
public ListNode reverseList(ListNode head) {
ListNode prev = null;
ListNode cur = head;
while(cur != null) {
ListNode nextNode = cur.next;
cur.next = prev;
prev = cur;
cur = nextNode;
}
return prev;
}
}