Delete A Node In Linked List using Python
Technology and generation are growing up together, and the younger generation users are somewhat connected with tech and the internet all the time. Not to mention, today the whole world in this time of crisis is working over the internet. But to make these technologies, software, etc a software developer must have excellent problem-solving skills. In this world where the internet is the new fuel, one needs to be pretty sharp. And by sharp, for software developers, it means knowing how to automate real-world problems using computer programs. Data structures help a lot in this journey of logic building. So today we’re going to write a simple data structure program to Delete A Node In Linked List using Python.
What is A Linked List?
A linked list is a basic list of nodes containing different elements for all algorithms in data structures. Also, it’s pretty easy to understand.
What’s The Approach?
- The main objective of deleting a linked list node is to free the memory allocated to a node so that it gets used instead of being wasted.
- To delete a given node firstly we’ll have to search for the node before it, as the previous node holds the address of the node we want to delete.
- Once we find the previous node, change its address section to hold the address of our to be deleted node is currently holding in its address section.
- Once the above instructions are done being executed, we’ll free the memory allocated to the deleted node.
Also Read: Divide Integers without Division Arithmetic Operator in Python
Python Program To Delete A Node In Linked List
Created Linked list is:
1 7 8 6
Linked List after Deletion :
7 8 6
# Python program to # demonstrate deletion in # linked list # Node class class Node: # Constructor to initialize the node object def __init__(self, data): self.data = data self.next = None class LinkedList: # Function to initialize head def __init__(self): self.head = None # Function to insert a new node at the beginning def push(self, new_data): new_node = Node(new_data) new_node.next = self.head self.head = new_node # Given a reference to the head of a list and a key, # delete the first occurrence of key in linked list def deleteNode(self, key): # Store head node temp = self.head # If head node itself holds the key to be deleted if (temp is not None): if (temp.data == key): self.head = temp.next temp = None return # Search for the key to be deleted, keep track of the # previous node as we need to change 'prev.next' while(temp is not None): if temp.data == key: break prev = temp temp = temp.next # if key was not present in linked list if(temp == None): return # Unlink the node from linked list prev.next = temp.next temp = None # Utility function to print the linked LinkedList def printList(self): temp = self.head while(temp): print (" %d" %(temp.data)), temp = temp.next # Driver program llist = LinkedList() llist.push(6) llist.push(8) llist.push(7) llist.push(1) print ("Created Linked List: ") llist.printList() llist.deleteNode(1) print ("\nLinked List after Deletion:") llist.printList()