forked from AdaGold/linked-list
-
Notifications
You must be signed in to change notification settings - Fork 68
/
Copy pathlinked_list.py
222 lines (189 loc) · 6.61 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
# Defines a node in the singly linked list
class Node:
def __init__(self, value, next_node = None):
self.value = value
self.next = next_node
# Defines the singly linked list
class LinkedList:
def __init__(self):
self.head = None # keep the head private. Not accessible outside this class
# returns the value in the first node
# returns None if the list is empty
# Time Complexity: O(1)
# Space Complexity: O(1)
def get_first(self):
if self.head:
return self.head.value
return None
# method to add a new node with the specific data value in the linked list
# insert the new node at the beginning of the linked list
# Time Complexity: O(1)
# Space Complexity: O(n)
def add_first(self, value):
new_first = Node(value, self.head)
new_first.next_node = self.head
self.head = new_first
# method to find if the linked list contains a node with specified value
# returns true if found, false otherwise
# Time Complexity: O(n)
# Space Complexity: O(1)
def search(self, value):
current = self.head
found = False
while found is False and current is not None:
if current.value == value:
found = True
else:
current = current.next
return found
# method that returns the length of the singly linked list
# Time Complexity: O(n)
# Space Complexity: O(1)
def length(self):
current = self.head
length = 0
while current:
length += 1
current = current.next
return length
# method that returns the value at a given index in the linked list
# returns None if there are fewer nodes in the linked list than the index value
# Time Complexity: O(n)
# Space Complexity: O(1)
def get_at_index(self, index):
current = self.head
i = 0
while i < index and current is not None:
i += 1
current = current.next
if i == index:
return current.value
return None
# method that returns the value of the last node in the linked list
# returns None if the linked list is empty
# Time Complexity: O(n)
# Space Complexity: O(1)
def get_last(self):
current = self.head
if not current:
return None
while current.next:
current = current.next
return current.value
# method that inserts a given value as a new last node in the linked list
# Time Complexity: O(n)
# Space Complexity: O(1)
def add_last(self, value):
new_last = Node(value)
if self.head is None:
self.head = new_last
return
else:
current = self.head
while current.next is not None:
current = current.next
current.next = new_last
# method to return the max value in the linked list
# returns the data value and not the node
def find_max(self):
current = self.head
if current is None:
return None
max = current.value
while current:
if max < current.value:
max = current.value
current = current.next
return max
# method to delete the first node found with specified value
# Time Complexity: O(n)
# (should be able to be done in O(1) if just getting rid of pointer?)
# Space Complexity: O(1)
def delete(self, value):
current = self.head
if current:
if current.value == value:
self.head = current.next
current = None
return
while current:
if current.value == value:
break
previous = current
current = current.next
if current == None:
return
previous.next = current.next
current = None
# method to print all the values in the linked list
# Time Complexity: O(n)
# Space Complexity: O(n)
def visit(self):
helper_list = []
current = self.head
while current:
helper_list.append(str(current.value))
current = current.next
print(", ".join(helper_list))
# method to reverse the singly linked list
# note: the nodes should be moved and not just the values in the nodes
# Time Complexity: O(n)
# Space Complexity: O(1)
def reverse(self):
previous = None
current = self.head
while current is not None:
next = current.next
current.next = previous
previous = current
current = next
self.head = previous
## Advanced/ Exercises
# returns the value at the middle element in the singly linked list
# Time Complexity: O(n)
# Space Complexity: O(1)
def find_middle_value(self):
if not self.head:
return None
return self.get_at_index(int(self.length()/2))
# find the nth node from the end and return its value
# assume indexing starts at 0 while counting to n
# Time Complexity: O(n)
# Space Complexity: O(1)
def find_nth_from_end(self, n):
current = self.head
if not current:
return None
len = self.length() - 1
if n > len:
return
for i in range(0, len - n):
current = current.next
return current.value
# checks if the linked list has a cycle. A cycle exists if any node in the
# linked list links to a node already visited.
# returns true if a cycle is found, false otherwise.
# Time Complexity: O(n)
# Space Complexity: O(n)
def has_cycle(self):
current = self.head
visited = set()
if current == None:
return False
while current.next != None:
visited.add(current)
current = current.next
if current in visited:
return True
return False
# Helper method for tests
# Creates a cycle in the linked list for testing purposes
# Assumes the linked list has at least one node
def create_cycle(self):
if self.head == None:
return
# navigate to last node
current = self.head
while current.next != None:
current = current.next
current.next = self.head # make the last node link to first node