链表反转核心代码:java
while(cur.next != null){ ListNode temp = cur.next; // cur.next = temp.next; // temp.next = pre.next; // pre.next = temp;//完成上面的四步 cur每次都停留在已经被反转的部分链表的末尾 }
基于原理:
Input: 1->2->3->4->5->NULL
实际上是有三个指针。顺序为pre cur temp, 咱们要作的就是把temp和cur互换ide