-
Notifications
You must be signed in to change notification settings - Fork 2
/
Sort-List.py
34 lines (30 loc) · 894 Bytes
/
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
def sort_list(left, right):
result = []
while ((len(left) > 0) and (len(right) > 0)):
if (left[0] <= right[0]):
result.append(left.pop(0))
else:
result.append(right.pop(0))
while (len(left) > 0):
result.append(left.pop(0))
while (len(right) > 0):
result.append(right.pop(0))
return result
class Solution(object):
def __init__(self):
self.list = []
def sortList(self, head):
if (type(head) != list):
h = []
while head:
h.append(head.val)
head = head.next
head = h
if (len(head) <= 1):
return head
middle = (len(head) / 2)
first = head[:middle]
second = head[middle:]
left = self.sortList(first)
right = self.sortList(second)
return sort_list(left, right)