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 ?

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

The Challenge

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.

1 3 5 7 Node to delete Can't access previous

The Solution: Copy and Skip Technique

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 ?

Algorithm Steps

  • 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.
Before: 1 3 5 7 After: 1 5 7 Copy value 5 Skip deleted node

Python Implementation

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):
    """Helper function to create a linked list from a list of elements"""
    if not elements:
        return None
    head = ListNode(elements[0])
    current = head
    for element in elements[1:]:
        current.next = ListNode(element)
        current = current.next
    return head

def print_list(head):
    """Helper function to print the linked list"""
    current = head
    result = []
    while current:
        result.append(str(current.val))
        current = current.next
    print(" ? ".join(result))

def find_node(head, value):
    """Helper function to find a node with given value"""
    current = head
    while current and current.val != value:
        current = current.next
    return current

def delete_node(node):
    """Delete a node from linked list given only access to that node"""
    if node is None or node.next is None:
        raise Exception("Cannot delete the last node or a null node with this method.")
    
    # Step 1: Copy the value from next node
    node.val = node.next.val
    
    # Step 2: Skip the next node
    node.next = node.next.next

# Example usage
elements = [1, 3, 5, 7, 9]
head = make_list(elements)

print("Original list:")
print_list(head)

# Find and delete node with value 3
node_to_delete = find_node(head, 3)
if node_to_delete:
    delete_node(node_to_delete)
    print("\nAfter deleting node with value 3:")
    print_list(head)
else:
    print("Node not found!")
Original list:
1 ? 3 ? 5 ? 7 ? 9

After deleting node with value 3:
1 ? 5 ? 7 ? 9

Time and Space Complexity

Aspect Complexity Explanation
Time Complexity O(1) Constant time operation
Space Complexity O(1) No extra space needed

Limitations

  • Cannot delete the last node − Since we need the next node to copy from, this method fails for the tail node.
  • Node reference required − You must have direct access to the node, not just its value.
  • Memory not freed immediately − The original next node becomes unreferenced but memory cleanup depends on garbage collection.

Conclusion

The copy-and-skip technique allows efficient O(1) deletion when you only have access to the node itself. This approach works by copying the next node's value and bypassing it, effectively "moving" the deletion to the next position.

Updated on: 2026-03-25T07:14:02+05:30

4K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements