forked from HarshCasper/NeoAlgo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
delete_node.py
55 lines (42 loc) · 1.29 KB
/
delete_node.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
class ListNode(): #Node Class
def __init__(self,value=None):
self.data = value
self.next = None
def create_LinkedList(head,val): # function for creating linked list
if(head.next!=None):
temp = head.next
while(temp.next!=None):
temp = temp.next
newnode = ListNode(val)
newnode.next = None
temp.next = newnode
else:
newnode = ListNode(val)
newnode.next = None
head.next = newnode
def print_linkedlist(head): #function for displaying linked list
temp = head.next
while(temp!=None):
print(temp.data,end =' ')
temp = temp.next
print()
def delete_node(node): #function to delete a node from linked list
if node.next is not None:
node.data, node.next = node.next.data, node.next.next
if __name__ == '__main__':
head = ListNode()
n = int(input("Enter total no of elemets in linked list :"))
for i in range(n):
create_LinkedList(head,int(input()))
print_linkedlist(head)
delete_node(head.next.next)
print_linkedlist(head)
# OUPUT :
# Enter total no of elemets in linked list :5
# 3
# 4
# 5
# 6
# 7
# 3 4 5 6 7
# 3 5 6 7