【LeetCode】連結串列 linked list(共34題)
【2】Add Two Numbers
【19】Remove Nth Node From End of List (2018年10月30日 演算法群)
給了一個連結串列,從尾部刪除第 N 個結點。
題解:two pointers,fast 先走 N 步,然後slow,fast 一起走,fast走到最後一個非空結點,slow就走到了要刪除結點的前一個結點。(注意有個特判情況是如果第N個結點是頭節點,那麼要特殊處理)
1 /*View Code* 2 * Definition for singly-linked list. 3 * struct ListNode { 4 * int val; 5 * ListNode *next; 6 * ListNode(int x) : val(x), next(NULL) {} 7 * }; 8 */ 9 class Solution { 10 public: 11 ListNode* removeNthFromEnd(ListNode* head, int n) { 12 if (!head) {return head;}13 ListNode *fast = head, *slow = head; 14 //1. fast goes n steps first 15 for (int k = 0; k < n && fast; ++k) { 16 fast = fast->next; 17 } 18 //2. both slow and fast move simultaneously until fast to the end of the list 19 if (fast) {20 while (fast->next) { 21 slow = slow->next; 22 fast = fast->next; 23 } 24 slow->next = slow->next->next; 25 } else { 26 head = head->next; 27 } 28 return head; 29 } 30 };
【21】Merge Two Sorted Lists
合併兩個有序連結串列變成一個大連結串列,大連結串列要求有序。(歸併排序)
題解:無,直接歸併
1 /** 2 * Definition for singly-linked list. 3 * struct ListNode { 4 * int val; 5 * ListNode *next; 6 * ListNode(int x) : val(x), next(NULL) {} 7 * }; 8 */ 9 class Solution { 10 public: 11 ListNode* mergeTwoLists(ListNode* l1, ListNode* l2) { 12 if (!l1) {return l2;} 13 if (!l2) {return l1;} 14 ListNode *p1 = l1, *p2 = l2, *head = 0, *tail = 0; 15 while (p1 && p2) { 16 if (p1->val < p2->val) { 17 if(!head) { 18 tail = head = p1; 19 } else { 20 tail = tail->next = p1; 21 } 22 p1 = p1->next; 23 } else { 24 if (!head) { 25 tail = head = p2; 26 } else { 27 tail = tail->next = p2; 28 } 29 p2 = p2->next; 30 } 31 } 32 //這裡其實不用遍歷了,直接連起來ok 33 if (p1) { 34 tail->next = p1; 35 } 36 if (p2) { 37 tail->next = p2; 38 } 39 return head; 40 } 41 };View Code
【23】Merge k Sorted Lists
給了k個已經排好序的連結串列,要返回一個綜合排序的大連結串列(歸併排序)
題解:用 prioprity_queue 的運算子()過載來實現比較函式。具體運算子過載的實現方法見程式碼。(奇怪的是為啥pq的cmp函式要寫 > ,pq裡面才是從小到大排序?)
這個題目其實應該複習 priority_queue 的比較函式的實現方法。(要搞懂原理。)
1 /** 2 * Definition for singly-linked list. 3 * struct ListNode { 4 * int val; 5 * ListNode *next; 6 * ListNode(int x) : val(x), next(NULL) {} 7 * }; 8 */ 9 class Solution { 10 public: 11 //好奇怪,為啥這裡要寫大於符號,pq才是從小到大排序... ???? 12 struct cmp{ 13 bool operator() (const ListNode* node1, const ListNode* node2) { 14 return node1->val > node2->val; 15 } 16 }; 17 18 ListNode* mergeKLists(vector<ListNode*>& lists) { 19 const int n = lists.size(); 20 if (n == 0) {return NULL;} 21 vector<ListNode*> ptr(n, NULL); 22 for (int i = 0; i < n; ++i) { 23 ptr[i] = lists[i]; 24 } 25 ListNode *head = 0, *tail = 0; 26 priority_queue<ListNode*, vector<ListNode*>, cmp> pq; 27 for (int i = 0; i < n; ++i) { 28 if (!ptr[i]) {continue;} //注意這裡有可能有的連結串列頭節點為空,要特判 29 pq.push(ptr[i]); 30 ptr[i] = ptr[i]->next; 31 } 32 while (!pq.empty()) { 33 ListNode* node = pq.top(); 34 pq.pop(); 35 if (node->next) { pq.push(node->next); } 36 if (!head) { 37 tail = head = node; 38 } else { 39 tail = tail->next = node; 40 } 41 } 42 return head; 43 } 44 };View Code
【24】Swap Nodes in Pairs
【25】Reverse Nodes in k-Group
【61】Rotate List
【82】Remove Duplicates from Sorted List II
【83】Remove Duplicates from Sorted List
【86】Partition List
【92】Reverse Linked List II
【109】Convert Sorted List to Binary Search Tree
【138】Copy List with Random Pointer
【141】Linked List Cycle
【142】Linked List Cycle II
【143】Reorder List
【147】Insertion Sort List
【148】Sort List
【160】Intersection of Two Linked Lists
【203】Remove Linked List Elements
【206】Reverse Linked List
【234】Palindrome Linked List
【237】Delete Node in a Linked List
【328】Odd Even Linked List
【369】Plus One Linked List
【379】Design Phone Directory
【426】Convert Binary Search Tree to Sorted Doubly Linked List
【430】Flatten a Multilevel Doubly Linked List
【445】Add Two Numbers II
【707】Design Linked List
【708】Insert into a Cyclic Sorted List
【725】Split Linked List in Parts
【817】Linked List Components
【876】Middle of the Linked List