Skip to content

Commit c2ee6da

Browse files
authored
sort_linkedlist_012
Initial File
1 parent 4c8c3c6 commit c2ee6da

1 file changed

Lines changed: 56 additions & 0 deletions

File tree

Sort_linkedlist_012

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
class LinkedList(object):
2+
def __init__(self):
3+
self.head = None
4+
5+
class Node(object):
6+
def __init__(self, data):
7+
self.data = data
8+
self.next = None
9+
10+
def sortList(self):
11+
count = [0, 0, 0]
12+
13+
temp = self.head
14+
while temp != None:
15+
count[temp.data] += 1
16+
temp = temp.next
17+
i = 0
18+
temp = self.head
19+
while temp != None:
20+
if count[i] == 0:
21+
i += 1
22+
else:
23+
temp.data = i
24+
count[i] -= 1
25+
temp = temp.next
26+
27+
def push(self, new_data):
28+
new_node = self.Node(new_data)
29+
new_node.next = self.head
30+
self.head = new_node
31+
32+
def printList(self):
33+
temp = self.head
34+
while temp != None:
35+
print(str(temp.data))
36+
temp = temp.next
37+
print('')
38+
39+
40+
llist = LinkedList()
41+
llist.push(0)
42+
llist.push(1)
43+
llist.push(0)
44+
llist.push(2)
45+
llist.push(1)
46+
llist.push(1)
47+
llist.push(2)
48+
llist.push(1)
49+
llist.push(2)
50+
51+
print("Linked List before sorting")
52+
llist.printList()
53+
llist.sortList()
54+
55+
print("Linked List after sorting")
56+
llist.printList()

0 commit comments

Comments
 (0)