LeetCode 链表题目中用到的高频核心代码块
链表反转核心代码:
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互换

浙公网安备 33010602011771号