-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdelete_singly_linked_list.py
More file actions
56 lines (46 loc) · 1.26 KB
/
Copy pathdelete_singly_linked_list.py
File metadata and controls
56 lines (46 loc) · 1.26 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
class Node:
def __init__(self):
self.data = None
self.next = None
def setData(self, data):
self.data = data
def getData(self):
return self.data
def setNext(self, next):
self.next = next
class SinglyLinkedList:
# constructor
def __init__(self):
self.head = None
# method for setting the head of the Linked List
def setHead(self, head):
self.head = head
# method for deleting a node having a certain data
def delete(self, data):
prev = None
current = self.head
while current != None:
if current.data == data:
if prev != None:
prev.next = current.next
return
else:
self.head = None
else:
prev = current
current = current.next
def print(self):
current = self.head
while current != None:
print(current.getData())
current = current.next
test = Node()
test.setData(3)
test2 = Node()
test2.setData(5)
test_list = SinglyLinkedList()
test_list.setHead(test)
test_list.head.next = test2
print('before', test_list.print())
test_list.delete(5)
print('after', test_list.print())