-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdelete_data_in_list.py
52 lines (44 loc) · 1012 Bytes
/
delete_data_in_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
class Node:
def __init__(self, data):
self.data = data
self.next = None
class LinkedList:
def __init__(self):
self.head = None
def printList(self):
temp = self.head
while temp is not None:
print(" %s" %(temp.data))
temp = temp.next
def deleteNode(self, value):
temp = self.head
if temp.data == value:
self.head = temp.next
temp = None
return
while temp is not None:
if temp.data == value:
break
prev = temp
temp = temp.next
if temp is None:
return
prev.next = temp.next
temp = None
l1 = LinkedList()
l1.head = Node('A')
l2 = Node('B')
l3 = Node('C')
l4 = Node('D')
l5 = Node('E')
l1.head.next = l2
l2.next = l3
l3.next = l4
l4.next = l5
print("before deletion: ")
l1.printList()
print("after deletion")
l1.deleteNode('A')
l1.deleteNode('C')
l1.deleteNode('E')
l1.printList()