forked from codehouseindia/Python-Programs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLinkedListdeletion.py
64 lines (44 loc) · 1.23 KB
/
LinkedListdeletion.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
# Python3 program to delete all
# the nodes of singly linked list
# Node class
class Node:
# Function to initialise the node object
def __init__(self, data):
self.data = data # Assign data
self.next = None # Initialize next as null
# Constructor to initialize the node object
class LinkedList:
# Function to initialize head
def __init__(self):
self.head = None
def deleteList(self):
# initialize the current node
current = self.head
while current:
prev = current.next # move next node
# delete the current node
del current.data
# set current equals prev node
current = prev
# push function to add node in front of llist
def push(self, new_data):
# Allocate the Node &
# Put in the data
new_node = Node(new_data)
# Make next of new Node as head
new_node.next = self.head
# Move the head to point to new Node
self.head = new_node
# Use push() to construct below
# list 1-> 12-> 1-> 4-> 1
if __name__ == '__main__':
llist = LinkedList()
llist.push(1)
llist.push(4)
llist.push(1)
llist.push(12)
llist.push(1)
print("Deleting linked list")
llist.deleteList()
print("Linked list deleted")
# This article is provided by Shrikant13