-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSortList.py
More file actions
41 lines (36 loc) · 1.04 KB
/
SortList.py
File metadata and controls
41 lines (36 loc) · 1.04 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
# 148. Sort List
# 归并排序
class ListNode:
def __init__(self, x):
self.val = x
self.next = None
class Solution:
def sortList(self, head):
if not head or not head.next:
return head
# 快慢指针,这里我们希望指向中点前一个点,所以fast初始为head.next
slow = head
fast = head.next
while fast and fast.next:
slow = slow.next
fast = fast.next.next
fast = slow.next
slow.next = None
dummy = self.merge(self.sortList(head), self.sortList(fast))
return dummy
def merge(self, left, right):
head = ListNode(0)
dummy = head
while left and right:
if left.val < right.val:
head.next = left
left = left.next
else:
head.next = right
right = right.next
head = head.next
if left:
head.next = left
if right:
head.next = right
return dummy.next