-
Notifications
You must be signed in to change notification settings - Fork 1
/
linked_list.py
59 lines (51 loc) · 1.6 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
class node:
def __init__(self, data):
self.data = data
self.next_pointer = None
class linked_list:
def __init__(self):
self.head = None
def print_list(self):
ptr = self.head
while ptr:
print(ptr.data)
ptr = ptr.next_pointer
def insert_at_start(self, data):
if self.head == None:
self.head = node(data)
else:
new_node = node(data)
new_node.next_pointer = self.head
self.head = new_node
def insert_at_end(self, data):
newNode = node(data)
temp = self.head
while(temp.next_pointer != None):
temp = temp.next_pointer
temp.next_pointer = newNode
def rm_node(self, data):
temp = self.head
if (temp.next_pointer is not None):
if(temp.data == data):
self.head = temp.next_pointer
return
else:
# else search all the nodes
while(temp.next_pointer != None):
if(temp.data == data):
break
prev = temp
temp = temp.next_pointer
if temp == None:
return
prev.next_pointer = temp.next_pointer
return
if __name__ == '__main__':
data1, data2, data3, = 50, 'A string', [6,7,8]
list_ = linked_list()
list_.insert_at_start(data1)
list_.insert_at_end(data2)
list_.insert_at_start(data3)
list_.print_list()
list_.rm_node(data3)
list_.print_list()