描述
Sort a linked list in O(n log n) time using constant space complexity.
分析
要求时间O(nlogn)
,上一题的插入排序就不能用了,可以使用归并排序,先split成两个链表,分别递归排序,再归并。可以用到我们之前的Merge Two Sorted List。
代码
归并排序
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
| class Solution(object): def mergeTwoLists(self, l1, l2): dummy = ListNode(-1) dummy.next = l1 prev = dummy while l1 and l2: if l1.val > l2.val: prev.next = l2 prev = l2 l2 = l2.next else: prev.next = l1 prev = l1 l1 = l1.next if not l1: prev.next = l2 else: prev.next = l1 return dummy.next
def splitList(self, head): h1 = head h2 = head.next p1 = h1 p2 = h2 while p2: p1.next = p2.next if p2.next: p2.next = p2.next.next p1 = p1.next p2 = p2.next return h1, h2
def sortList(self, head): """ :type head: ListNode :rtype: ListNode """ if not head or not head.next: return head l1, l2 = self.splitList(head) l1 = self.sortList(l1) l2 = self.sortList(l2) return self.mergeTwoLists(l1, l2)
|