1class Node:
2 def __init__(self):
3 self.data = None
4 self.next = None
5
6class LinkedList:
7 def __init__(self):
8 self.head = None
9
10 def addNode(self, data):
11 curr = self.head
12 if curr is None:
13 n = Node()
14 n.data = data
15 self.head = n
16 return
17
18 if curr.data > data:
19 n = Node()
20 n.data = data
21 n.next = curr
22 self.head = n
23 return
24
25 while curr.next is not None:
26 if curr.next.data > data:
27 break
28 curr = curr.next
29 n = Node()
30 n.data = data
31 n.next = curr.next
32 curr.next = n
33 return
34
35 def __str__(self):
36 data = []
37 curr = self.head
38 while curr is not None:
39 data.append(curr.data)
40 curr = curr.next
41 return "[%s]" %(', '.join(str(i) for i in data))
42
43 def __repr__(self):
44 return self.__str__()
45
46def main():
47 ll = LinkedList()
48 num = int(input("Enter a number: "))
49 while num != -1:
50 ll.addNode(num)
51 num = int(input("Enter a number: "))
52 c = ll.head
53 while c is not None:
54 print(c.data)
55 c = c.next