forked from netsetos/python_code
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSort_linkedlist_012
56 lines (47 loc) · 1.13 KB
/
Sort_linkedlist_012
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
class LinkedList(object):
def __init__(self):
self.head = None
class Node(object):
def __init__(self, data):
self.data = data
self.next = None
def sortList(self):
count = [0, 0, 0]
temp = self.head
while temp != None:
count[temp.data] += 1
temp = temp.next
i = 0
temp = self.head
while temp != None:
if count[i] == 0:
i += 1
else:
temp.data = i
count[i] -= 1
temp = temp.next
def push(self, new_data):
new_node = self.Node(new_data)
new_node.next = self.head
self.head = new_node
def printList(self):
temp = self.head
while temp != None:
print(str(temp.data))
temp = temp.next
print('')
llist = LinkedList()
llist.push(0)
llist.push(1)
llist.push(0)
llist.push(2)
llist.push(1)
llist.push(1)
llist.push(2)
llist.push(1)
llist.push(2)
print("Linked List before sorting")
llist.printList()
llist.sortList()
print("Linked List after sorting")
llist.printList()