📄 Need a professional CV? Try our Resume Builder! Get Started

Deletion in a Single Linked List

This blog explains the different techniques to delete nodes in a single linked list and provides Python code examples for better understanding. These operations are crucial for memory management in dynamic data structures and are essential in algorithmic problem-solving.

Sun Jan 12, 2025

Linked List Deletion Techniques

"Removing nodes in a linked list is essential for efficient data structure manipulation." — James Chapman

In this article, we will explore different techniques for deleting nodes in a single linked list. We will walk through the process of deleting nodes at the beginning, end, and at an intermediate position, using Python code examples to demonstrate each operation.

1. Introduction to Linked List Deletion

What is Linked List Deletion?

Deletion in a linked list involves removing a node from the list. The process requires us to adjust the pointers of the surrounding nodes so that they no longer point to the node being deleted. Deleting nodes at various positions in the list requires different approaches.

2. Delete a Node at the Beginning

Deletion Method: To delete a node at the beginning:

  1. Access the current head node.
  2. Update the head pointer to the next node.
  3. Delete the old head node.

Time Complexity: O(1) — This operation is constant time as it only requires updating the head pointer.

Example in Python:


def delete_at_beginning(self):
    if not self.head:  # Empty list check
        return None
    temp = self.head
    self.head = self.head.next  # Update head to second node
    temp = None  # Delete first node
                        
Delete Node at Beginning Initial State 10 20 30 head After Deletion 10 20 30 head

3. Delete a Node at the End

Deletion Method (for non-empty linked lists):

  1. Traverse to the last node of the list.
  2. Update the second-to-last node’s next pointer to null.

Time Complexity: O(n) — This operation requires traversing the entire list to find the last node, which results in linear time complexity.

Example in Python:


def delete_at_end(self):
    if not self.head:  # Empty list check
        return None
    if self.head.next is None:  # Single node in list
        self.head = None
        return
    iter = self.head
    while iter.next.next:  # Traverse until second last node
        iter = iter.next
    iter.next = None  # Remove the last node
                        
Delete Node at End Initial State 10 20 30 head After Deletion 10 20 → None 30 head

4. Delete a Node at an Intermediate Position (by Index)

Deletion Method: To delete a node at a specific position:

  1. Check if the index is valid. If not, raise an exception.
  2. If the index is `0`, use the `delete_at_beginning` method.
  3. For other positions, traverse the list to find the node just before the position, then update the pointer to bypass the node to be deleted.

Time Complexity: O(n) — This operation requires traversal to the desired index, making it linear in terms of the number of nodes.

Example in Python:


def delete_at_index(self, index):
    if index < 0 or index >= self.get_length():  # Invalid index check
        raise Exception("Invalid index")
    if index == 0:
        self.delete_at_beginning()
        return
    iter = self.head
    count = 0
    while iter:
        if count == index - 1:
            iter.next = iter.next.next  # Bypass the node to be deleted
            break
        iter = iter.next
        count += 1
                        
Delete Node at Middle (Index 1) Initial State 10 20 30 head After Deletion 10 20 30 head

5. Final Thoughts

Efficiency Considerations:

  • Deletion at the beginning is efficient with constant time complexity O(1).
  • Deletion at the end or at an intermediate position requires traversal, resulting in linear time complexity O(n).

These deletion operations are fundamental for managing memory efficiently in dynamic data structures and are crucial for a variety of algorithms.