1. 程式人生 > 實用技巧 >LeetCode 連結串列題總結

LeetCode 連結串列題總結

最近花了幾天時間,將連結串列題除帶鎖外的題目做完了,下面對連結串列題部分題目進行總結和分析。

連結串列題目解決方法

1、連結串列反轉

2、快慢指標

連結串列反轉模板

ListNode cur = head.next;
head.next = null;
while (cur != null) {
       ListNode next = cur.next;
       cur.next = head;
       head = cur;
       cur = next;
}

快慢指標模板

ListNode slow = head;
ListNode fast 
= head; ListNode prev = slow; while(fast != null && fast.next != null){ prev =slow; slow = slow.next; fast = fast.next.next; }

部分題目分類

1. 簡單題

1290 Convert Binary Number in a Linked List to Integer

237 Delete Node in a Linked List

21 Merge Two Sorted Lists

1669 Merge In Between Linked List

2. 連結串列反轉

206 Reverse Linked List

24 Swap Nodes in Pairs

92 Reverse Linked List II

25 Reverse Nodes in k-Group

3. 快慢指標

876 Middle of the Linked List

141 Linked List Cycle

142 Linked List Cycle II