Skip to content

Commit 4489b63

Browse files
authored
Added Linked List
1 parent 04608d0 commit 4489b63

File tree

1 file changed

+79
-0
lines changed

1 file changed

+79
-0
lines changed

Linked_List.py

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
class Linked_List:
2+
3+
def __init__(self,val):
4+
self.value = val
5+
self.next = None
6+
7+
def insert_first(self,head,val):
8+
new_node = Linked_List(val)
9+
new_node.next = head
10+
return new_node
11+
12+
def insert_last(self,head,val):
13+
new_node = Linked_List(val)
14+
temp = head
15+
while temp.next!=None:
16+
temp = temp.next
17+
temp.next = new_node
18+
return head
19+
20+
def insert_mid(self,head,val,pos):
21+
new_node = Linked_List(val)
22+
temp = head
23+
position = 0
24+
while (position!=pos):
25+
temp = temp.next
26+
position += 1
27+
next_node = temp.next
28+
temp.next = new_node
29+
new_node.next = next_node
30+
return head
31+
32+
def del_first(self,head):
33+
temp = head
34+
head = head.next
35+
temp.next = None
36+
return head
37+
38+
def del_last(self,head):
39+
temp = head
40+
while temp.next.next!=None:
41+
temp = temp.next
42+
temp.next = None
43+
return head
44+
45+
def del_mid(self,head,pos):
46+
temp = head
47+
position = 0
48+
while (position!=pos):
49+
temp = temp.next
50+
position += 1
51+
del_node = temp.next
52+
temp.next = temp.next.next
53+
del_node.next = None
54+
return head
55+
56+
def traverse(self,head):
57+
temp = head
58+
while (temp):
59+
print(temp.value, end = ' ')
60+
temp = temp.next
61+
62+
63+
# Driver Code
64+
List = Linked_List(1)
65+
head = List
66+
head = List.insert_first(head,0)
67+
head = List.insert_last(head,3)
68+
head = List.insert_mid(head,2,1)
69+
head = List.insert_mid(head,4,2)
70+
List.traverse(head)
71+
72+
head = List.del_first(head)
73+
List.traverse(head)
74+
75+
head = List.del_last(head)
76+
List.traverse(head)
77+
78+
head = List.del_mid(head,0)
79+
List.traverse(head)

0 commit comments

Comments
 (0)