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): = data = 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) = 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 ( == key):
                self.head =
                temp = None

        # Search for the key to be deleted, keep track of the
        # previous node as we need to change ''
        while(temp is not None):
            if == key:
            prev = temp
            temp =

        # if key was not present in linked list
        if(temp == None):

        # Unlink the node from linked list =

        temp = None

    # Utility function to print the linked LinkedList
    def printList(self):
        temp = self.head
            print (" %d" %(,
            temp =

# Driver program
llist = LinkedList()

print ("Created Linked List: ")


print ("\nLinked List after Deletion:")



I'm a coding geek interested in cyberspace who loves to write and read

Leave a Reply

Your email address will not be published. Required fields are marked *