-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathq0148.py
More file actions
72 lines (65 loc) · 2.05 KB
/
q0148.py
File metadata and controls
72 lines (65 loc) · 2.05 KB
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
#!/usr/bin/python3
from typing import List
class ListNode:
def __init__(self, val=0, next=None):
self.val = val
self.next = next
class Solution:
def sortList(self, head: ListNode) -> ListNode:
def merge_list(node1: ListNode, node2: ListNode) -> ListNode:
pre = ListNode(0)
p = pre
while node1 and node2:
if node1.val <= node2.val:
p.next = node1
node1 = node1.next
else:
p.next = node2
node2 = node2.next
p = p.next
if node1:
p.next = node1
if node2:
p.next = node2
return pre.next
def sort_helper(node: ListNode) -> ListNode:
if node is None or node.next is None:
return node
slow = node
fast = node.next
while fast and fast.next:
fast = fast.next.next
slow = slow.next
new_node = slow.next
slow.next = None
head1 = sort_helper(node)
head2 = sort_helper(new_node)
return merge_list(head1, head2)
return sort_helper(head)
def sortList2(self, head: ListNode) -> ListNode:
if not head:
return head
p = head
list: List[ListNode] = []
while p:
list.append(p)
p = p.next
length = len(list)
for i in range(1, length):
left = 0
right = i - 1
index = i
while left <= right:
mid = (left + right) // 2
if (list[i].val < list[mid].val):
index = mid
right = mid - 1
else:
left = mid + 1
if i != index:
node = list.pop(i)
list.insert(index, node)
for i in range(length - 1):
list[i].next = list[i + 1]
list[length - 1].next = None
return list[0]