卖萌的弱渣

I am stupid, I am hungry.

Sort List

Sort a linked list in O(n log n) time using constant space complexity.

Solution

  • Merge Sort
(Sort-List.py) download
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# Definition for singly-linked list.
# class ListNode(object):
#     def __init__(self, x):
#         self.val = x
#         self.next = None

class Solution(object):
    def sortList(self, head):
        """
        :type head: ListNode
        :rtype: ListNode
        """
        if head == None or head.next==None:
            return head

        # merge sort
        fast_node = head
        slow_node = head

        # find pivot
        while fast_node.next != None and fast_node.next.next != None:
            slow_node = slow_node.next
            fast_node = fast_node.next.next

        middle = slow_node.next
        slow_node.next = None

        # split
        leftNode = self.sortList(head)
        rightNode = self.sortList(middle)

        return self.merge(leftNode,rightNode)

    # merge
    def merge(self,left,right):
        if left != None and right == None:
            return left
        if left == None and right != None:
            return right
        if left == None and righ == None:
            return None

        # find head 
        ret = None
        if left.val < right.val:
            ret = left
            left = left.next
        else:
            ret = right
            right = right.next

        # merge every node
        head = ret
        while left != None and right != None:
            if left.val < right.val:
                head.next = left
                left = left.next
            else:
                head.next = right
                right = right.next
            head = head.next

        if left != None:
            head.next = left
        if right != None:
            head.next = right

        return ret