Sort a linked list in O(n log n) time using constant space complexity.html
Example 1:git
Input: 4->2->1->3 Output: 1->2->3->4
Example 2:github
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,直到還原整個長度。這裏將鏈表從中間斷開的方法,採用的就是快慢指針,你們可能對快慢指針找鏈表中的環比較熟悉,其實找鏈表中的中點一樣好使,由於快指針每次走兩步,慢指針每次走一步,當快指針到達鏈表末尾時,慢指針正好走到中間位置,參見代碼以下:ide
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 解法一:post
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函數中也使用了遞歸,這樣使代碼更加簡潔啦~ui
C++ 解法二:url
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 解法二:spa
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
相似題目:
參考資料:
https://leetcode.com/problems/sort-list/description/
https://leetcode.com/problems/sort-list/discuss/46857/clean-and-short-merge-sort-solution-in-c