-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLinked_List.py
More file actions
79 lines (66 loc) · 1.56 KB
/
Linked_List.py
File metadata and controls
79 lines (66 loc) · 1.56 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
class Linked_List:
def __init__(self,val):
self.value = val
self.next = None
def insert_first(self,head,val):
new_node = Linked_List(val)
new_node.next = head
return new_node
def insert_last(self,head,val):
new_node = Linked_List(val)
temp = head
while temp.next!=None:
temp = temp.next
temp.next = new_node
return head
def insert_mid(self,head,val,pos):
new_node = Linked_List(val)
temp = head
position = 0
while (position!=pos):
temp = temp.next
position += 1
next_node = temp.next
temp.next = new_node
new_node.next = next_node
return head
def del_first(self,head):
temp = head
head = head.next
temp.next = None
return head
def del_last(self,head):
temp = head
while temp.next.next!=None:
temp = temp.next
temp.next = None
return head
def del_mid(self,head,pos):
temp = head
position = 0
while (position!=pos):
temp = temp.next
position += 1
del_node = temp.next
temp.next = temp.next.next
del_node.next = None
return head
def traverse(self,head):
temp = head
while (temp):
print(temp.value, end = ' ')
temp = temp.next
# Driver Code
List = Linked_List(1)
head = List
head = List.insert_first(head,0)
head = List.insert_last(head,3)
head = List.insert_mid(head,2,1)
head = List.insert_mid(head,4,2)
List.traverse(head)
head = List.del_first(head)
List.traverse(head)
head = List.del_last(head)
List.traverse(head)
head = List.del_mid(head,0)
List.traverse(head)