dummy -> 2 ->1 ->3
主要思想:把下一个元素插入dummy和已经reversed的序列之间。
class Solution {
public ListNode reverseList(ListNode head) {
if(head == null || head.next == null) {
return head;
}
ListNode dummy = new ListNode(0), next = head;
dummy.next = head;
while(head.next != null) {
next = dummy.next;
dummy.next = head.next;
head.next = head.next.next;
dummy.next.next = next;
}
return dummy.next;
}
}