-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlinked_list.py
More file actions
86 lines (70 loc) · 2.7 KB
/
Copy pathlinked_list.py
File metadata and controls
86 lines (70 loc) · 2.7 KB
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
'''
Linked List
A linked list is a datastructure where each node holds a value and a reference to the next node. It is like a chain
where one can find a value through another node, traversing the chain until a value is found. The linked list is slow
when we either want to remove/find/add an element in the middle O(N), or if we want to remove the very last element.
Remember, we cannot traverse backwards and to remove an element is like 'unhooking' its links, and so we must have the
element before whatever element we want to remove. As such removing the last element is always of time complexity N.
'''
class node:
def __init__(self, val = None):
self.value = val
self.nextVal = None
class linked_list:
def __init__(self):
self.bot_node = None
self.top_node = None
self.size = 0
def __len__(self):
return self.size
def insertion(self, val):
new_node = node(val)
if(self.size > 0):
self.top_node.nextVal = new_node
else:
self.bot_node = new_node
self.top_node = new_node
self.size += 1
# Element wise print of the linked list:
def element_wise_print(self):
currentNode = self.bot_node
print(currentNode.value)
for i in range(1, self.size):
currentNode = currentNode.nextVal
print(currentNode.value)
def remove_first(self):
currentNode = self.bot_node
self.bot_node = currentNode.nextVal
self.size -= 1
return currentNode.value
def find_element(self, val):
current_node = self.bot_node
for i in range(self.size):
if(current_node.value == val):
return True
current_node = current_node.nextVal
return False
def replace_on_index(self,val,i):
assert self.size > i, "Linked List Insertion: The index has to be within the list."
insertion_node = node(val)
index = 0
current_node = self.bot_node
while(index -1 < i):
current_node = current_node.nextVal
index += 1
insertion_node.nextVal = current_node.nextVal
current_node.nextVal = insertion_node
def remove_on_index(self, index):
assert self.size > index, "Unvalid operation, linked list out of bounds."
current_node = self.bot_node
i = 0
while(not i+1 == index):
current_node = current_node.nextVal
i+= 1
if(i+2 == self.size):
current_node.nextVal = None
else:
current_node.nextVal = (current_node.nextVal).nextVal
self.size -= 1
def remove_last(self):
self.remove_on_index(self.size-1)