-
Notifications
You must be signed in to change notification settings - Fork 0
/
SortList.py
40 lines (37 loc) · 969 Bytes
/
SortList.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
from ListNode import ListNode, createList, printList
import sys
class Solution(object):
def sortList(self, head):
"""
:type head: ListNode
:rtype: ListNode
"""
if not head:
return None
if not head.next:
return head
p1 = head
p2 = head
while p2.next and p2.next.next:
p1 = p1.next
p2 = p2.next.next
t = p1.next
p1.next = None
return self.merge(self.sortList(head), self.sortList(t))
def merge(self, l1, l2):
if (not l1) and l2:
return l2
if (not l2) and l1:
return l1
if l1.val < l2.val:
l1.next = self.merge(l1.next, l2)
return l1
else:
l2.next = self.merge(l1, l2.next)
return l2
def main():
c = Solution()
l = createList([1])
printList(c.sortList(l))
if __name__ == '__main__':
main()