LeetCode 鏈表題目中用到的高頻核心代碼塊

鏈表反轉核心代碼: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