-
Notifications
You must be signed in to change notification settings - Fork 0
/
linkedlist_5.py
45 lines (35 loc) · 1.11 KB
/
linkedlist_5.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
#Deletion of node of the linked list
class Node:
def __init__(self,dataVal=None):
self.dataval=dataVal
self.nextval=None
class LinkedList:
def __init__(self):
self.headval=None
def traverse(self):
printval=S.headval
while(printval!=None):
print(printval.dataval)
printval=printval.nextval
def deleteNode(self,data):
currentnode=self.headval
while(currentnode.dataval!=data):
#print(currentnode.dataval)
p=currentnode
currentnode=currentnode.nextval
#print(currentnode.dataval)
#print(p.dataval)
p.nextval=currentnode.nextval
S=LinkedList()
S.headval=Node(1)
q=Node(2)
r=Node(3)
S.headval.nextval=q
q.nextval=r
t=Node(4)
r.nextval=t
print("before Deletion")
S.traverse()
S.deleteNode(2)
print("After deletion ")
S.traverse()