-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathlinked-list-construction.py
162 lines (144 loc) · 4.82 KB
/
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
# Linked List Construction
# 🟠 Medium
#
# https://www.algoexpert.io/questions/linked-list-construction
#
# Tags: Linked List - Design
import timeit
# This is an input class. Do not edit.
class Node:
def __init__(self, value):
self.value = value
self.prev = None
self.next = None
# A doubly linked list class.
class DoublyLinkedList:
def __init__(self):
self.head = None
self.tail = None
# Time complexity: O(1)
# Space complexity: O(1)
def setHead(self, node):
if self.head is None:
self.head = node
self.tail = node
return
self.insertBefore(self.head, node)
# Time complexity: O(1)
# Space complexity: O(1)
def setTail(self, node):
if self.tail is None:
self.setHead(node)
return
self.insertAfter(self.tail, node)
# Time complexity: O(1)
# Space complexity: O(1)
def insertBefore(self, node, nodeToInsert):
if nodeToInsert is self.head and nodeToInsert is self.tail:
return
self.remove(nodeToInsert)
nodeToInsert.prev = node.prev
nodeToInsert.next = node
if node.prev:
node.prev.next = nodeToInsert
else:
self.head = nodeToInsert
node.prev = nodeToInsert
# self.printAsList()
# Time complexity: O(1)
# Space complexity: O(1)
def insertAfter(self, node, nodeToInsert):
if nodeToInsert is self.head and nodeToInsert is self.tail:
return
self.remove(nodeToInsert)
nodeToInsert.prev = node
nodeToInsert.next = node.next
if node.next:
node.next.prev = nodeToInsert
else:
self.tail = nodeToInsert
node.next = nodeToInsert
# Time complexity: O(k) - We need to iterate over k positions to
# find the node at that position, then we use O(1) methods.
# Space complexity: O(1)
def insertAtPosition(self, position, nodeToInsert):
if position == 1:
self.setHead(nodeToInsert)
return
node = self.head
currentPosition = 1
while node and currentPosition != position:
node = node.next
currentPosition += 1
if node:
self.insertBefore(node, nodeToInsert)
else:
self.setTail(nodeToInsert)
# Time complexity: O(n) - We need to iterate over n nodes to
# find the node with the given value, n could be the entire linked
# list, then we use O(1) methods.
# Space complexity: O(1)
def removeNodesWithValue(self, value):
# print(f"» Removing node with value {value}")
node = self.head
while node:
current = node
node = node.next
if current.value == value:
self.remove(current)
# Time complexity: O(1)
# Space complexity: O(1)
def remove(self, node):
# print(f"» Removing node {node.value}")
if node is self.head:
self.head = self.head.next
if node is self.tail:
self.tail = self.tail.prev
if node.prev:
node.prev.next = node.next
if node.next:
node.next.prev = node.prev
node.prev = None
node.next = None
# Time complexity: O(n) - We need to iterate over n nodes to
# find the node with the given value, n could be the entire linked
# list, then we use O(1) methods.
# Space complexity: O(1)
def containsNodeWithValue(self, value):
# print(f"» Checking contains node with value {value}")
current = self.head
while current:
if current.value == value:
return True
current = current.next
return False
# Time complexity: O(n) - We iterate the entire list.
# Space complexity: O(n) - We store the values in a list that later
# we join and print.
def printAsList(self):
res = []
current = self.head
while current:
res.append(str(current.value))
current = current.next
print(" " + " » ".join(res))
def test():
executors = [DoublyLinkedList]
tests = [] # TODO add tests.
for executor in executors:
start = timeit.default_timer()
for _ in range(1):
for col, t in enumerate(tests):
sol = executor()
result = sol.methodCall(t[0])
exp = t[1]
assert result == exp, (
f"\033[93m» {result} <> {exp}\033[91m for"
+ f" test {col} using \033[1m{executor.__name__}"
)
stop = timeit.default_timer()
used = str(round(stop - start, 5))
cols = "{0:20}{1:10}{2:10}"
res = cols.format(executor.__name__, used, "seconds")
print(f"\033[92m» {res}\033[0m")
test()