-
Notifications
You must be signed in to change notification settings - Fork 19
/
1.4. doubly- linked-list-construction.py
342 lines (303 loc) · 8.71 KB
/
1.4. doubly- linked-list-construction.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
337
338
339
340
341
342
# Feel free to add new properties and methods to the class.
# noinspection PyPep8Naming
class Node:
def __init__(self, value):
self.value = value
self.next = None
self.prev = None
def __repr__(self):
return str(self.value)
def get_prev_value(self):
if self.prev is None:
return 'None'
return str(self.prev.value)
def get_next_value(self):
if self.next is None:
return 'None'
return str(self.next.value)
def __str__(self):
return 'Node(prev:' + self.get_prev_value() + ', this:' + str(
self.value) + ', next:' + self.get_next_value() + ')'
def __eq__(self, other):
if other:
return self.value == other.value
return self is None
# noinspection PyPep8Naming
class DoublyLinkedList:
def __init__(self):
self.head = None
self.tail = None
def setHead(self, node):
# Write your code here.
if self.head is None:
self.head = node
self.tail = node
return
self.insertBefore(self.head, node)
def setTail(self, node):
# Write your code here.
if self.tail is None:
self.setHead(node)
return
self.insertAfter(self.tail, node)
def append(self, node):
if self.head is None:
self.setHead(node)
return
current = self.head
while current.next is not None:
current = current.next
node.prev = current
current.next = node
def insertBefore(self, node, nodeToInsert):
# Write your code here.
if nodeToInsert == self.head and nodeToInsert == self.tail:
return
nodeToInsert.prev = node.prev
nodeToInsert.next = node
if node.prev is None:
self.head = nodeToInsert
else:
node.prev.next = nodeToInsert
node.prev = nodeToInsert
def insertAfter(self, node, nodeToInsert):
# Write your code here.
if nodeToInsert == self.head and nodeToInsert == self.tail:
return
nodeToInsert.prev = node
nodeToInsert.next = node.next
if node.next is None:
self.tail = nodeToInsert
else:
node.next.prev = nodeToInsert
node.next = nodeToInsert
def insertAtPosition(self, position, nodeToInsert):
# Write your code here.
if position == 1:
self.setHead(nodeToInsert)
return
current = self.head
index = 1
while current and position != index:
index += 1
current = current.next
if current is None:
self.setTail(nodeToInsert)
else:
self.insertBefore(current, nodeToInsert)
def removeNodesWithValue(self, value):
# Write your code here.
current = self.head
while current:
nxt = current.next
if current.value == value:
self.remove(current)
current = nxt
def remove(self, node):
# Write your code here.
if node == self.head:
self.head = self.head.next
if node == self.tail:
self.tail = self.tail.prev
self.removeNode(node)
def removeNode(self, node):
if node.next:
node.next.prev = node.prev
if node.prev:
node.prev.next = node.next
del node
def containsNodeWithValue(self, value):
# Write your code here.
current = self.head
while current:
if current.value == value:
return True
current = current.next
return False
def __repr__(self):
if self.head is None:
return '---<Empty>---'
statement = ''
current = self.head
while current:
statement += str(current)
current = current.next
if current:
statement += ' ---> '
return statement + ''
# Initial Wrong solution
# class DoublyLinkedList:
# def __init__(self):
# self.head = None
# self.tail = None
#
# def setHead(self, node):
# # Write your code here.
#
# if self.head is not None:
# self.head.prev = node
#
# node.next = self.head
# self.head = node
#
# def setTail(self, node):
# # Write your code here.
#
# if self.head is None:
# self.head = node
#
# if self.tail is not None:
# self.tail.next = node
#
# node.prev = self.tail
# self.tail = node
# return self.head
#
# def append(self, node):
# if self.head is None:
# self.setHead(node)
# return
#
# current = self.head
# while current.next is not None:
# current = current.next
#
# node.prev = current
# current.next = node
#
# def insertBefore(self, node, nodeToInsert):
# # Write your code here.
# if node == self.head:
# self.setHead(nodeToInsert)
# return
#
# current = self.head
# while current.next:
# if current.next == node:
# nodeToInsert.prev = current
# nodeToInsert.next = current.next
#
# current.next.prev = nodeToInsert
# current.next = nodeToInsert
# break
# current = current.next
#
# def insertAfter(self, node, nodeToInsert):
# # Write your code here.
# current = self.head
# while current.next:
# if current.next == node:
# nodeToInsert.prev = current.next
# nodeToInsert.next = current.next.next
#
# if current.next.next:
# current.next.next.prev = nodeToInsert
# current.next.next = nodeToInsert
# break
# current = current.next
#
# def insertAtPosition(self, position, nodeToInsert):
# # Write your code here.
# if position == 1:
# self.setHead(nodeToInsert)
# return
# current = self.head
# index = 1
# while current:
# if position == index + 1:
# nodeToInsert.prev = current
# nodeToInsert.next = current.next
# if current.next:
# current.next.prev = nodeToInsert
# current.next = nodeToInsert
# break
# index += 1
# current = current.next
#
# def removeNodesWithValue(self, value):
# # Write your code here.
# current = self.head
# while current:
# if current.value == value:
# if current.prev:
# current.prev.next = current.next
# if current.next:
# current.next.prev = current.prev
#
# current = current.next
#
# def remove(self, node):
# # Write your code here.
# current = self.head
# while current:
# if current == node:
# if current.prev:
# current.prev.next = current.next
# if current.next:
# current.next.prev = current.prev
# break
#
# current = current.next
#
# def containsNodeWithValue(self, value):
# # Write your code here.
# if self.head is None:
# return False
#
# current = self.head
# while current:
# if current.value == value:
# return True
# current = current.next
# return False
#
# def __repr__(self):
# if self.head is None:
# return '---<Empty>---'
#
# statement = ''
# current = self.head
# while current:
# statement += str(current)
# current = current.next
# if current:
# statement += ' ---> '
#
# return statement + ''
#
#
# if __name__ == '__main__':
# doubly = DoublyLinkedList()
#
# doubly.append(Node(1))
# doubly.append(Node(2))
# doubly.append(Node(4))
# # print(doubly)
#
# doubly.insertBefore(Node(4), Node(3))
# # print(doubly)
#
# doubly.insertAfter(Node(4), Node(5))
# # print(doubly)
#
# doubly.remove(Node(3))
# # print(doubly)
#
# doubly.append(Node(2))
# doubly.append(Node(2))
# # print(doubly)
#
# doubly.removeNodesWithValue(2)
# print(doubly)
#
# doubly.insertAtPosition(2, Node(2))
# print(doubly)
#
# doubly.insertAtPosition(3, Node(3))
# print(doubly)
#
# doubly.insertAtPosition(6, Node(6))
# print(doubly)
#
# doubly.insertAtPosition(2, Node(1.5))
# print(doubly)