-
Notifications
You must be signed in to change notification settings - Fork 0
/
148. Sort List.py
47 lines (36 loc) · 1.17 KB
/
148. 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
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, val=0, next=None):
# self.val = val
# self.next = next
class Solution:
def sortList(self, head: Optional[ListNode]) -> Optional[ListNode]:
if not head or not head.next:
return head
def find_mid(node):
slow = node
fast = node.next
while fast and fast.next:
slow = slow.next
fast = fast.next.next
return slow
def merge(left, right):
temp = ListNode(0)
node = temp
while left and right:
if left.val < right.val:
node.next = left
left = left.next
else:
node.next = right
right = right.next
node = node.next
node.next = left if left else right
return temp.next
mid = find_mid(head)
left = head
right = mid.next
mid.next = None
left = self.sortList(left)
right = self.sortList(right)
return merge(left, right)