-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path0147-insertion-sort-list.py
48 lines (36 loc) · 1.55 KB
/
0147-insertion-sort-list.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
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, val=0, next=None):
# self.val = val
# self.next = next
class Solution:
def insertionSortList(self, head: Optional[ListNode]) -> Optional[ListNode]:
'''
1. if the value is less than the head value then add it to the first list
2. else move the head ptr to next and compare
4, 2, 1, 3
1. 4 > 2 2. 2 > 1
2, 4, 1, 3 1, 2, 4, 3
3. 1 > 3 False 4. 2 > 3 False 5. 4 > 3
1, 2, 4, 3 1, 2, 4, 3 1, 2, 3, 4
^ ^
| |
temp temp
'''
res = ListNode()
res.next = head
tail = res.next
curr = tail.next
while curr:
if curr.val >= tail.val:
tail = curr
curr = curr.next
else:
tail.next = curr.next
pos = res
while pos.next.val < curr.val:
pos = pos.next
curr.next = pos.next
pos.next = curr
curr = tail.next
return res.next