-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path148. Sort List.py
64 lines (53 loc) · 1.59 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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# notworking wait to fix
class ListNode:
def __init__(self, val=None, next=None):
self.val = val
self.next = next
class SingleLinkList:
def __init__(self):
self.head = None
self.tail = None
self.size = 0
def append(self, val):
new_node = ListNode(val)
if self.head == None:
self.head = new_node
self.tail = new_node
else:
self.tail.next = new_node
self.tail = self.tail.next
self.size += 1
def sort(self):
for i in range(self.size,0,-1):
now = self.head
prev = self.head
for j in range(0, i-1, 1):
if(now.val > now.next.val):
tmp = now.next
now.next = tmp.next
tmp.next = now
if(now == self.head):
self.head = tmp
prev = tmp
else:
prev.next = tmp
prev = prev.next
else:
now = now.next
if(j != 0):
prev = prev.next
def to_list(self):
result = []
i = self.head
while(i != None):
result.append(i.val)
i = i.next
return result
class Solution:
def mergeTwoLists(self, list1, list2):
linklist = SingleLinkList()
for i in list1 + list2:
linklist.append(i)
linklist.sort()
print(linklist.to_list())
a = Solution().mergeTwoLists([1,2,4],[1,3,4])