卖萌的弱渣

I am stupid, I am hungry.

Insertation Sort List

Sort a linked list using insertion sort.

Solution

(Inserttion-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
# Definition for singly-linked list.
# class ListNode(object):
#     def __init__(self, x):
#         self.val = x
#         self.next = None

class Solution(object):
    def insertionSortList(self, head):
        """
        :type head: ListNode
        :rtype: ListNode
        """

        if head == None or head.next == None:
            return head
        new_node = ListNode(-sys.maxint-1)
        # new_node.next = head
        new_node.next = head
        head = new_node
        # tail: biggest element current sorted list
        tail = head


        while tail.next:

            cur = tail.next
            # case: just assign new node after the tail
            if cur.val >= tail.val:
                tail = tail.next
            else: # case: need to insert the node again
                # find the place
                prev = head
                new_cur = prev.next
                while new_cur.val < cur.val:
                    new_cur = new_cur.next
                    prev = prev.next
                # remove the node
                tail.next = cur.next
                # insert the node again
                cur.next = prev.next
                prev.next = cur

        return head.next
                                                                                                                                                                                                                                                                                                           return head.