Skip to main content

Delete a Node in Linked List

Problem Statement:​

  • Example:



βœ… Solution: Pointer Manipulation with Dummy Node​

SinglyLinkedListNode* deleteNode(SinglyLinkedListNode* llist, int position) {
// Create a dummy node pointing to the head to simplify deletion logic
SinglyLinkedListNode *dummy = new SinglyLinkedListNode(0);
dummy->next = llist;

SinglyLinkedListNode *curr = dummy;

// Traverse to the node just before the one to delete
for(int i = 0; i < position; i++) {
if(curr->next == NULL) return dummy->next; // position out of bounds
curr = curr->next;
}

// Bypass the node to delete
if(curr->next != NULL)
curr->next = curr->next->next;

// Return the updated list (skipping dummy node)
return dummy->next;
}


πŸ“ How It Works​

  • A dummy node is used to simplify edge cases like deleting the head node.
  • Traverse to the node just before the target position.
  • Change its next pointer to skip over the node at the given position.
  • Return dummy->next as the new head.

🧩 Key Logic​

There’s no recurrence here β€” it's purely pointer manipulation:

curr->next = curr->next->next;

This bypasses the node at the position.


⏱️ Time & Space Complexity​

MetricValue
πŸ•’ TimeO(position)
🧠 SpaceO(1) β€” constant extra space

⚠️ Edge Cases​

  • Deleting from an empty list (llist == NULL)
  • Deleting at position 0 (head node)
  • Deleting a node at an invalid position (beyond list size)

πŸ’‘ Other Approaches​

ApproachProsCons
Without dummy nodeSaves one allocationNeeds special case for deleting head ❌
With dummy node βœ…Uniform logicSlight extra memory

  • Insert Node at Head/Tail/Position
  • Delete Node by Value
  • Reverse a Linked List
  • Remove N-th Node From End of List (LeetCode 19)

πŸ’¬

Discussion & Doubts