forked from iamshubhamg/Leet-Code
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlinked_list.py
65 lines (46 loc) · 1.18 KB
/
linked_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
53
54
55
56
57
58
59
60
61
62
63
64
65
class Node:
def __init__(self, data):
self.data = data
self.next = None
class LinkedList:
def __init__(self):
self.head = None
def push(self, newdata):
newnode = Node(newdata)
newnode.next = self.head
self.head = newnode
def delete_node(self, key):
global prev
temp = self.head
if temp is not None:
if temp.data == key:
self.head = temp.next
temp = None
return
while temp is not None:
if temp.data == key:
break
prev = temp
temp = temp.next
if temp is None:
return
prev.next = temp.next
temp = None
def print_list(self):
temp = self.head
while temp:
print(temp.data, end=" ")
temp = temp.next
if __name__ == "__main__":
llst = LinkedList()
llst.head = Node(2)
second = Node(3)
third = Node(4)
llst.head.next = second
second.next = third
llst.push(1)
print("Before deletion")
llst.print_list()
llst.delete_node(3)
print("\nAfter deletion")
llst.print_list()