Delete Node in a Linked List in Python



A linked list is a linear data structure where each element is a separate object, commonly referred to as a node. Each node contains two fields: the data and a pointer to the next node in the list. In this article, we'll learn how to delete a node from a singly linked list using Python.

Suppose we have a linked list with a few elements. Our task is to delete a specific node, given only access to that node - not the head of the list. For example:

Input:  1 ? 3 ? 5 ? 7 ? 9  
Delete: 3  
Output: 1 ? 5 ? 7 ? 9

Deleting Node in a Linked List in Python

In a singly linked list, each node points only to its next node, and there is no way to go backward. Therefore, if we are given only a reference to a specific node that we need to delete, we cannot access or modify the previous node's next pointer.

To work around this limitation, we use a clever technique: instead of actually removing the current node, we copy the data from the next node into the current node and then bypass (unlink) the next node. This gives the illusion that the current node has been deleted from the list.

Here are the two key steps to perform this operation:

  • node.val = node.next.val : Copies the value from the next node into the current node.
  • node.next = node.next.next : Updates the current node's next pointer to skip over the next node.

By doing this, the node that originally followed the given node is removed from the list, and the list remains well-formed. However, this approach cannot be used if the given node is the last node in the list, since there is no "next node" to copy from.

Python Program to Delete Node in a Linked List

Here is a complete implementation in Python that demonstrates how to delete a node from a linked list when you only have access to that node:

class ListNode:
    def __init__(self, data, next=None):
        self.val = data
        self.next = next

def make_list(elements):
    head = ListNode(elements[0])
    current = head
    for element in elements[1:]:
        current.next = ListNode(element)
        current = current.next
    return head

def print_list(head):
    current = head
    print('[', end="")
    while current:
        print(current.val, end=", " if current.next else "")
        current = current.next
    print(']')

def find_node(head, value):
    current = head
    while current and current.val != value:
        current = current.next
    return current

def delete_node(node):
    if node is None or node.next is None:
        raise Exception("Cannot delete the last node or a null node with this method.")
    node.val = node.next.val
    node.next = node.next.next

# Example usage
head = make_list([1, 3, 5, 7, 9])
node_to_delete = find_node(head, 3)
delete_node(node_to_delete)
print_list(head)

When you run the above code, the output will be:

[1, 5, 7, 9]

Note: This method only works if the node to be deleted is not the last node in the linked list. Since we copy the data from the next node, we need that next node to exist.

Updated on: 2025-07-31T12:49:23+05:30

4K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements