C++實現LeetCode(148.鏈表排序)
[LeetCode] 148. Sort List 鏈表排序
Sort a linked list in O(n log n) time using constant space complexity.
Example 1:
Input: 4->2->1->3
Output: 1->2->3->4
Example 2:
Input: -1->5->3->4->0
Output: -1->0->3->4->5
常見排序方法有很多,插入排序,選擇排序,堆排序,快速排序,冒泡排序,歸並排序,桶排序等等。。它們的時間復雜度不盡相同,而這裡題目限定瞭時間必須為O(nlgn),符合要求隻有快速排序,歸並排序,堆排序,而根據單鏈表的特點,最適於用歸並排序。為啥呢?這是由於鏈表自身的特點決定的,由於不能通過坐標來直接訪問元素,所以快排什麼的可能不太容易實現(但是被評論區的大神們打臉,還是可以實現的),堆排序的話,如果讓新建結點的話,還是可以考慮的,若隻能交換結點,最好還是不要用。而歸並排序(又稱混合排序)因其可以利用遞歸來交換數字,天然適合鏈表這種結構。歸並排序的核心是一個 merge() 函數,其主要是合並兩個有序鏈表,這個在 LeetCode 中也有單獨的題目 Merge Two Sorted Lists。由於兩個鏈表是要有序的才能比較容易 merge,那麼對於一個無序的鏈表,如何才能拆分成有序的兩個鏈表呢?我們從簡單來想,什麼時候兩個鏈表一定都是有序的?就是當兩個鏈表各隻有一個結點的時候,一定是有序的。而歸並排序的核心其實是分治法 Divide and Conquer,就是將鏈表從中間斷開,分成兩部分,左右兩邊再分別調用排序的遞歸函數 sortList(),得到各自有序的鏈表後,再進行 merge(),這樣整體就是有序的瞭。因為子鏈表的遞歸函數中還是會再次拆成兩半,當拆到鏈表隻有一個結點時,無法繼續拆分瞭,而這正好滿足瞭前面所說的“一個結點的時候一定是有序的”,這樣就可以進行 merge 瞭。然後再回溯回去,每次得到的都是有序的鏈表,然後進行 merge,直到還原整個長度。這裡將鏈表從中間斷開的方法,采用的就是快慢指針,大傢可能對快慢指針找鏈表中的環比較熟悉,其實找鏈表中的中點同樣好使,因為快指針每次走兩步,慢指針每次走一步,當快指針到達鏈表末尾時,慢指針正好走到中間位置,參見代碼如下:
C++ 解法一:
class Solution { public: ListNode* sortList(ListNode* head) { if (!head || !head->next) return head; ListNode *slow = head, *fast = head, *pre = head; while (fast && fast->next) { pre = slow; slow = slow->next; fast = fast->next->next; } pre->next = NULL; return merge(sortList(head), sortList(slow)); } ListNode* merge(ListNode* l1, ListNode* l2) { ListNode *dummy = new ListNode(-1); ListNode *cur = dummy; while (l1 && l2) { if (l1->val < l2->val) { cur->next = l1; l1 = l1->next; } else { cur->next = l2; l2 = l2->next; } cur = cur->next; } if (l1) cur->next = l1; if (l2) cur->next = l2; return dummy->next; } };
Java 解法一:
public class Solution { public ListNode sortList(ListNode head) { if (head == null || head.next == null) return head; ListNode slow = head, fast = head, pre = head; while (fast != null && fast.next != null) { pre = slow; slow = slow.next; fast = fast.next.next; } pre.next = null; return merge(sortList(head), sortList(slow)); } public ListNode merge(ListNode l1, ListNode l2) { ListNode dummy = new ListNode(-1); ListNode cur = dummy; while (l1 != null && l2 != null) { if (l1.val < l2.val) { cur.next = l1; l1 = l1.next; } else { cur.next = l2; l2 = l2.next; } cur = cur.next; } if (l1 != null) cur.next = l1; if (l2 != null) cur.next = l2; return dummy.next; } }
下面這種方法也是歸並排序,而且在merge函數中也使用瞭遞歸,這樣使代碼更加簡潔啦~
C++ 解法二:
class Solution { public: ListNode* sortList(ListNode* head) { if (!head || !head->next) return head; ListNode *slow = head, *fast = head, *pre = head; while (fast && fast->next) { pre = slow; slow = slow->next; fast = fast->next->next; } pre->next = NULL; return merge(sortList(head), sortList(slow)); } ListNode* merge(ListNode* l1, ListNode* l2) { if (!l1) return l2; if (!l2) return l1; if (l1->val < l2->val) { l1->next = merge(l1->next, l2); return l1; } else { l2->next = merge(l1, l2->next); return l2; } } };
Java 解法二:
public class Solution { public ListNode sortList(ListNode head) { if (head == null || head.next == null) return head; ListNode slow = head, fast = head, pre = head; while (fast != null && fast.next != null) { pre = slow; slow = slow.next; fast = fast.next.next; } pre.next = null; return merge(sortList(head), sortList(slow)); } public ListNode merge(ListNode l1, ListNode l2) { if (l1 == null) return l2; if (l2 == null) return l1; if (l1.val < l2.val) { l1.next = merge(l1.next, l2); return l1; } else { l2.next = merge(l1, l2.next); return l2; } } }
Github 同步地址:
https://github.com/grandyang/leetcode/issues/148
類似題目:
Merge Two Sorted Lists
Sort Colors
Insertion Sort List
參考資料:
https://leetcode.com/problems/sort-list/description/
https://leetcode.com/problems/sort-list/discuss/46857/clean-and-short-merge-sort-solution-in-c
https://leetcode.com/problems/sort-list/discuss/46937/56ms-c-solutions-using-quicksort-with-explanations
https://leetcode.com/problems/sort-list/discuss/46772/i-have-a-pretty-good-mergesort-method-can-anyone-speed-up-the-run-time-or-reduce-the-memory-usage
到此這篇關於C++實現LeetCode(148.鏈表排序)的文章就介紹到這瞭,更多相關C++實現鏈表排序內容請搜索WalkonNet以前的文章或繼續瀏覽下面的相關文章希望大傢以後多多支持WalkonNet!
推薦閱讀:
- C++實現LeetCode(147.鏈表插入排序)
- C++實現LeetCode(109.將有序鏈表轉為二叉搜索樹)
- C++實現LeetCode(141.單鏈表中的環)
- C++實現LeetCode(203.移除鏈表元素)
- C++數據結構之鏈表詳解