-
-
Notifications
You must be signed in to change notification settings - Fork 298
/
Copy path147.py
48 lines (41 loc) · 1.5 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
__________________________________________________________________________________________________
sample 48 ms submission
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, x):
# self.val = x
# self.next = None
class Solution:
def insertionSortList(self, head: ListNode) -> ListNode:
node_vec = []
while head:
node_vec.append(head.val)
head = head.next
node_vec = sorted(node_vec)
curr = new_node = ListNode(0)
for i in range(len(node_vec)):
curr.next = ListNode(node_vec[i])
curr = curr.next
return new_node.next
__________________________________________________________________________________________________
sample 14796 kb submission
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, x):
# self.val = x
# self.next = None
class Solution:
def insertionSortList(self, head: ListNode) -> ListNode:
sentinel = ListNode(-1)
prev = sentinel
while head:
nxt = head.next
if prev.val >= head.val:
prev = sentinel
while prev.next and prev.next.val < head.val:
prev = prev.next
head.next = prev.next
prev.next = head
head = nxt
return sentinel.next
__________________________________________________________________________________________________