forked from Muhammad-Waleed-Khalid/problem-solving
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SLL.py
336 lines (299 loc) · 10.5 KB
/
SLL.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
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
from SLLNode import LinkedListNode
class LinkedList:
# Default Constructor
def __init__(self) -> None:
self.length = 0
self.head = None
# Constructor with array of values
def __init__(self, values ) -> None:
self.head = None
self.length = 0
for value in values:
self.insert_at_tail(value)
# Insert node at head
def insert_at_head(self, value):
new_node = LinkedListNode(value)
new_node.next = self.head
self.head = new_node
self.length += 1
# Insert node at tail
def insert_at_tail(self, value):
new_node = LinkedListNode(value)
if self.head is None:
self.head = new_node
self.length += 1
return
current_node = self.head
while current_node.next:
current_node = current_node.next
current_node.next = new_node
self.length += 1
# Insert node at position
def insert_at_position(self, value, position):
new_node = LinkedListNode(value)
if position == 0:
new_node.next = self.head
self.head = new_node
self.length += 1
return
current_node = self.head
for _ in range(position-1):
current_node = current_node.next
new_node.next = current_node.next
current_node.next = new_node
self.length += 1
# Delete node at head
def delete_node_at_head(self):
if self.head is None:
raise Exception('Linked List is empty')
self.head = self.head.next
self.length -= 1
# Delete node at tail
def delete_node_at_tail(self):
if self.head is None:
raise Exception('Linked List is empty')
if self.head.next is None:
self.head = None
self.length -= 1
return
current_node = self.head
while current_node.next.next:
current_node = current_node.next
current_node.next = None
self.length -= 1
# Delete node at position
def delete_node_at_position(self, position):
if self.head is None:
raise Exception('Linked List is empty')
if position == 0:
self.head = self.head.next
self.length -= 1
return
current_node = self.head
for _ in range(position-1):
if current_node.next is None:
raise Exception('Position is out of range')
current_node = current_node.next
current_node.next = current_node.next.next
self.length -= 1
# Create a deep copy of the linked list
def create_deep_copy(self):
if self.head is None:
return None
current_node = self.head
new_linked_list = LinkedList()
while current_node:
new_linked_list.insert_at_tail(current_node.value)
current_node = current_node.next
return new_linked_list
# print the linked list
def print_list(self):
print(str(self))
# Convert the linked list to a string
def __str__(self):
current_node = self.head
string = '['
while current_node:
if current_node.next is None:
string += str(current_node.value)
break
string += str(current_node.value) + ', '
current_node = current_node.next
string += ']'
return string
# Convert the linked list to a list
def to_list(self):
current_node = self.head
list = []
while current_node:
list.append(current_node.value)
current_node = current_node.next
return list
# Convert the linked list to a list
def __List__(self):
return self.to_list()
def __len__(self):
return self.length
def __iter__(self):
self.current_node = self.head
return self
def __next__(self):
self.current_node = self.current_node.next
return self.current_node
def __getitem__(self, index):
if index >= self.length:
raise Exception('Index out of range')
current_node = self.head
for _ in range(index):
current_node = current_node.next
return current_node.value
def __setitem__(self, index, value):
if index >= self.length:
raise Exception('Index out of range')
current_node = self.head
for _ in range(index):
current_node = current_node.next
current_node.value = value
def __delitem__(self, index):
self.delete_node_at_position(index)
def __contains__(self, value):
current_node = self.head
while current_node:
if current_node.value == value:
return True
current_node = current_node.next
return False
def __search__(self, value):
current_node = self.head
i = 0
while current_node:
if current_node.value == value:
return i
current_node = current_node.next
i += 1
return -1
def __add__(self, other):
if not isinstance(other, LinkedList):
raise Exception('Cannot add non-LinkedList object')
new_linked_list = LinkedList()
current_node = self.head
while current_node:
new_linked_list.insert_at_tail(current_node.value)
current_node = current_node.next
current_node = other.head
while current_node:
new_linked_list.insert_at_tail(current_node.value)
current_node = current_node.next
return new_linked_list
def __reverse__(self):
current_node = self.head
prev_node = None
next_node = None
while current_node:
next_node = current_node.next
current_node.next = prev_node
prev_node = current_node
current_node = next_node
self.head = prev_node
def delete_value(self, value):
if self.head is None:
raise Exception('Linked List is empty')
if self.head.value == value:
self.head = self.head.next
self.length -= 1
return
current_node = self.head
while current_node.next:
if current_node.next.value == value:
current_node.next = current_node.next.next
self.length -= 1
return
current_node = current_node.next
raise Exception('Value not found')
def delete_values(self, value):
if self.head is None:
raise Exception('Linked List is empty')
while self.head.value == value:
self.head = self.head.next
self.length -= 1
current_node = self.head
while current_node.next:
if current_node.next.value == value:
current_node.next = current_node.next.next
self.length -= 1
else:
current_node = current_node.next
def __eq__(self, other):
if not isinstance(other, LinkedList):
raise Exception('Cannot compare non-LinkedList object')
if self.length != other.length:
return False
current_node = self.head
other_node = other.head
while current_node:
if current_node.value != other_node.value:
return False
current_node = current_node.next
other_node = other_node.next
return True
def __ne__(self, other):
return not self.__eq__(other)
def __lt__(self, other):
if not isinstance(other, LinkedList):
raise Exception('Cannot compare non-LinkedList object')
if self.length >= other.length:
return False
current_node = self.head
other_node = other.head
while current_node:
if current_node.value >= other_node.value:
return False
current_node = current_node.next
other_node = other_node.next
return True
def __le__(self, other):
if not isinstance(other, LinkedList):
raise Exception('Cannot compare non-LinkedList object')
if self.length > other.length:
return False
current_node = self.head
other_node = other.head
while current_node:
if current_node.value > other_node.value:
return False
current_node = current_node.next
other_node = other_node.next
return True
def __gt__(self, other):
if not isinstance(other, LinkedList):
raise Exception('Cannot compare non-LinkedList object')
if self.length <= other.length:
return False
current_node = self.head
other_node = other.head
while current_node:
if current_node.value <= other_node.value:
return False
current_node = current_node.next
other_node = other_node.next
return True
def __ge__(self, other):
if not isinstance(other, LinkedList):
raise Exception('Cannot compare non-LinkedList object')
if self.length < other.length:
return False
current_node = self.head
other_node = other.head
while current_node:
if current_node.value < other_node.value:
return False
current_node = current_node.next
other_node = other_node.next
return True
def __copy__(self):
return self
def __deepcopy__(self):
return self.create_deep_copy()
def __hash__(self):
return hash(self.to_list())
def __sub__(self, other):
if not isinstance(other, LinkedList):
raise Exception('Cannot subtract non-LinkedList object')
new_linked_list = LinkedList()
current_node = self.head
while current_node:
if current_node.value not in other:
new_linked_list.insert_at_tail(current_node.value)
current_node = current_node.next
return new_linked_list
def __sub_eq__(self, other):
if not isinstance(other, LinkedList):
raise Exception('Cannot subtract non-LinkedList object')
current_node = self.head
while current_node:
if current_node.value in other:
self.delete_value(current_node.value)
current_node = current_node.next
def __delete__(self):
self.head = None
self.length = 0