-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path148. Sort List.py
More file actions
46 lines (42 loc) · 1.22 KB
/
148. Sort List.py
File metadata and controls
46 lines (42 loc) · 1.22 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
# Definition for singly-linked list.
# class ListNode(object):
# def __init__(self, x):
# self.val = x
# self.next = None
class Solution(object):
def sortList(self, head):
"""
:type head: ListNode
:rtype: ListNode
"""
if not head or head.next == None: #要是只有head的话
return head
slow = head
fast = head.next
while fast and fast.next:
slow = slow.next
fast = fast.next.next
mid = slow.next
slow.next = None
return self.merge_sorted_linked_list(self.sortList(head),self.sortList(mid))
def merge_sorted_linked_list(self,l1,l2):
if l1 == None:
return l2
if l2 == None:
return l1
dummy = ListNode(0)
temp = dummy
while l1 and l2:
if l1.val < l2.val:
temp.next = l1
l1 = l1.next
else:
temp.next = l2
l2 = l2.next
temp = temp.next
if l1:
temp.next = l1
if l2:
temp.next = l2
return dummy.next
#把linklist排序,找到中点,排好序,再merge