-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLinked_List.py
308 lines (256 loc) · 5.02 KB
/
Linked_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
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
class Node :
def __init__(self,data):
self.data = data
self.next = None
class LinkedList:
def __init__(self):
self.head = None
def printlist(self):
cur_node = self.head
while cur_node:
print(cur_node.data)
cur_node = cur_node.next
def append(self,data):
new_node = Node(data)
if self.head is None:
self.head = new_node
return
last_node = self.head
while last_node.next:
last_node = last_node.next
last_node.next = new_node
def preppend(self,data):
new_node = Node(data)
new_node.next = self.head
self.head = new_node
def insert_after_node(self,previous_node,data):
if not previous_node:
print("Previous Node does not exist")
return
new_node = Node(data)
new_node.next = previous_node.next
previous_node.next = new_node
def delete_node(self, key):
#delete head node
cur_node = self.head
if cur_node and cur_node.data == key:
self.head = cur_node.next
return
#delete node which is not head node
prev = None
while cur_node and cur_node.data != key:
prev = cur_node
cur_node = cur_node.next
if cur_node is None:
return
prev.next = cur_node.next
cur_node = None
def delete_node_at_pos(self,pos):
if self.head:
cur_node = self.head
if pos == 0:
self.head = cur_node.next
cur_node = None
return
prev = None
count = 0
while cur_node and count != pos:
prev = cur_node
cur_node = cur_node.next
count += 1
if cur_node is None:
return
prev.next = cur_node.next
cur_node = None
def len_of_list(self):
cur_node = self.head
count = 0
while cur_node:
count += 1
cur_node = cur_node.next
return count
#Swapping two nodes based on their values
def swap_nodes(self,key1,key2):
if key1 == key2:
return
prev1 = None
cur1 = self.head
while cur1 and cur1.data != key1:
prev1 = cur1
cur1 = cur1.next
prev2 = None
cur2 = self.head
while cur2 and cur2.data != key2:
prev2 = cur2
cur2 = cur2.next
if not cur1 or not cur2:
return
if prev1 :
prev1.next = cur2
else:
self.head = cur2
if prev2 :
prev2.next = cur1
else:
self.head = cur1
cur1.next,cur2.next = cur2.next,cur1.next
# Reversing nodes of a linked list
def reverse_nodes(self):
prev = None
cur = self.head
while cur:
nxt = cur.next
cur.next = prev
prev = cur
cur = nxt
self.head = prev
def merge_lists(self, list1):
p = self.head
q = list1.head
s = None
if not p:
return q
if not q:
return p
if p and q:
if p.data <= q.data:
s= p
p = s.next
else:
s = q
q = s.next
new_head = s
while p and q:
if p.data <= q.data:
s.next = p
s = p
p = s.next
else:
s.next = q
s= q
q = s.next
if not p:
s.next = q
if not q:
s.next = p
self.head = new_head
return self.head
def remove_duplicates(self):
prev = None
cur = self.head
duplicates = dict()
while cur:
if cur.data in duplicates:
prev.next = cur.next
cur = None
else:
duplicates[cur.data] = 1
prev = cur
cur = prev.next
def nth_node(self,n):
total_length = self.len_of_list()
cur = self.head
while cur :
if total_length == n:
print(cur.data)
return cur.data
total_length -= 1
cur = cur.next
if cur is None :
return
def count_occurences(self,data):
count = 0
cur = self.head
while cur :
if cur.data == data:
count += 1
cur = cur.next
return count
def rotate(self,k):
if self.head and self.head.next :
p = self.head
q = self.head
prev = None
count = 0
while p and count < k:
prev = p
p = p.next
q = q.next
count += 1
p = prev
while q :
prev = q
q = q.next
q = prev
q.next = self.head
self.head = p.next
p.next = None
def is_palindrome(self):
s =" "
p = self.head
while p:
s += p.data
p = p.next
return s == s[::-1]
llist = LinkedList()
llist.append("A")
llist.append("B")
llist.append("C")
llist.append("K")
llist.append("H")
llist.append("G")
llist.append("F")
llist.preppend("D")
llist.printlist()
print(llist.is_palindrome())
print("\n")
llist.swap_nodes("C","F")
llist.printlist()
print(llist.len_of_list())
llist.insert_after_node(llist.head.next,"E")
print("\n")
llist.delete_node("D")
llist.delete_node("C")
llist.delete_node_at_pos(2)
print("\n")
llist.printlist()
print(llist.len_of_list())
print("\n")
llist.reverse_nodes()
llist.printlist()
list1 = LinkedList()
list2 = LinkedList()
list1.append(1)
list1.append(5)
list1.append(7)
list1.append(9)
list1.append(10)
list2.append(2)
list2.append(3)
list2.append(4)
list2.append(6)
list2.append(8)
print("\n")
list1.printlist()
print("\n")
list2.printlist()
print('\n')
list1.merge_lists(list2)
list1.printlist()
print("\n")
list3 = LinkedList()
list3.append(1)
list3.append(6)
list3.append(2)
list3.append(2)
list3.append(4)
list3.append(1)
list3.rotate(3)
list3.printlist()
print("\n")
print(list3.count_occurences(2))
print("\n")
list3.remove_duplicates()
list3.printlist()
print("\n Nth node os list 3")
list3.nth_node(3)