-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path147.py
75 lines (65 loc) · 1.82 KB
/
147.py
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
69
70
71
72
73
74
75
# Definition for singly-linked list.
class ListNode(object):
def __init__(self, x):
self.val = x
self.next = None
class Solution(object):
def printNone(self, head):
while(head is not None):
print(head.val)
head=head.next
def insertionSortList(self, head):
"""
:type head: ListNode
:rtype: ListNode
"""
if head is None:
return None
if head.next is None:
return head
if head.next.next is None:
if head.val > head.next.val:
head.next.next = head
t = head.next
head.next = None
return t
else:
return head
start = head
end = head
curr = end.next
while(curr is not None):
if curr.val > end.val:
end = curr
curr=curr.next
elif curr.val <= start.val:
end.next = curr.next
curr.next = start
start = curr
curr=end.next
else:
tmp = start
prev = None
while tmp.val<curr.val:
prev = tmp
tmp=tmp.next
end.next = curr.next
curr.next = prev.next
prev.next = curr
curr=end.next
print("---------")
self.printNone(start)
print("---------")
return start
if __name__=="__main__":
s = Solution()
# l = ListNode(4)
# l.next = ListNode(2)
# l.next.next = ListNode(1)
# l.next.next.next = ListNode(3)
# s.insertionSortList(l)
l = ListNode(0)
l.next = ListNode(0)
l.next.next = ListNode(0)
l.next.next.next = ListNode(0)
s.insertionSortList(l)