-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathll.py
104 lines (78 loc) · 1.8 KB
/
ll.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
class Node:
def __init__(self, data, next = None):
self.data = data
self.next = next
class LinkedList:
traversal_cost = "O(n)"
def __init__(self, node=None):
self.head = node
def addNode(self, data):
if(self.head == None):
self.head = Node(data)
return
newNode = Node(data)
temp = self.head
while temp.next != None:
temp = temp.next
temp.next = newNode
print(temp.data)
return
def displayList(self):
temp = self.head
while temp != None:
print(temp.data, " ")
temp = temp.next
return
def addToGivenIndex(self, data, index):
print("Assuming index starts from 0")
currNode = self.head
prevNode = None
while index > 0:
prevNode = currNode
currNode = currNode.next
index -= 1
newNode = Node(data)
prevNode.next = newNode
newNode.next = currNode
return
def deleteNodeGivenIndex(self, index):
print("Assuming index starts from 0 and deleting node of index:", index)
currNode = self.head
prevNode = None
while index > 0:
prevNode = currNode
currNode = currNode.next
index-=1
prevNode.next = currNode.next
return
def reverse_list(self):
print("List before reversing it:")
self.displayList()
currNode = self.head
prevNode = None
while currNode != None:
nextNode = currNode.next
currNode.next = prevNode
prevNode = currNode
currNode = nextNode
self.head = prevNode;
print("List after reversing it:")
self.displayList()
return
nodeA = Node(1)
ll = LinkedList(nodeA)
nodeB = Node("Mon")
nodeC = Node(3.4)
nodeA.next = nodeB
nodeB.next = nodeC
print(ll.head.data)
ll2 = LinkedList(nodeB)
print(nodeB.next.data)
print(ll.traversal_cost, " ", ll2.traversal_cost)
ll.addNode("d")
ll.displayList()
ll.addToGivenIndex(35.6, 2)
ll.displayList()
ll.deleteNodeGivenIndex(3)
ll.displayList()
ll.reverse_list()