Delete a Node from Circular Linked List in DSA C++ - Time & Space Complexity
When deleting a node from a circular linked list, we want to know how the time needed changes as the list grows.
We ask: How many steps does it take to find and remove the node?
Analyze the time complexity of the following code snippet.
void deleteNode(Node*& head, int key) {
if (!head) return;
Node *curr = head, *prev = nullptr;
do {
if (curr->data == key) {
if (prev) prev->next = curr->next;
else {
// Find the last node to update its next pointer
Node* last = head;
while (last->next != head) {
last = last->next;
}
head = curr->next;
last->next = head;
}
delete curr;
return;
}
prev = curr;
curr = curr->next;
} while (curr != head);
}
This code searches the circular linked list for a node with the given key and deletes it if found.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Looping through nodes to find the target node.
- How many times: Up to n times, where n is the number of nodes in the list.
As the list grows, the number of steps to find the node grows roughly the same as the list size.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | Up to 10 checks |
| 100 | Up to 100 checks |
| 1000 | Up to 1000 checks |
Pattern observation: The steps grow linearly with the number of nodes.
Time Complexity: O(n)
This means the time to delete a node grows in direct proportion to the list size.
[X] Wrong: "Deleting a node in a circular linked list always takes constant time O(1)."
[OK] Correct: You must first find the node, which can take up to n steps, so the overall time depends on list size.
Understanding this helps you explain how linked list operations scale and shows you can analyze real data structure behaviors clearly.
"What if the list was doubly linked? How would the time complexity for deletion change?"
Practice
Solution
Step 1: Understand single-node circular list
A circular linked list with one node points to itself.Step 2: Delete the only node
Removing this node leaves no nodes, so head becomes null.Final Answer:
The list becomes empty (head points to null). -> Option AQuick Check:
Single node deleted = empty list [OK]
- Assuming list still has one node after deletion
- Not updating head to null
- Confusing circular with linear list after deletion
key from a circular linked list in C++?Solution
Step 1: Understand pointer passing in C++
To modify the head pointer itself, pass it by reference (Node*& head).Step 2: Check parameter order and types
Function should accept head reference first, then key to delete.Final Answer:
void deleteNode(Node*& head, int key) { /* code */ } -> Option CQuick Check:
Pass head by reference to modify it [OK]
- Passing head by value (won't update original list)
- Wrong parameter order
- Passing head as object, not pointer or reference
Solution
Step 1: Identify node to delete
Node with value 20 is between 10 and 30.Step 2: Remove node 20 and link 10 to 30
Update 10's next pointer to 30, maintaining circular link.Final Answer:
10 -> 30 -> (back to 10) -> Option BQuick Check:
Deleted 20, linked 10 to 30 [OK]
- Not updating previous node's next pointer
- Breaking circular link
- Deleting wrong node
void deleteNode(Node*& head, int key) {
if (!head) return;
Node* curr = head;
while (curr->next != head && curr->next->data != key) {
curr = curr->next;
}
if (curr->next->data == key) {
Node* temp = curr->next;
curr->next = temp->next;
delete temp;
}
}Solution
Step 1: Analyze deletion logic
Code searches for node before target and deletes next node.Step 2: Check head deletion case
If node to delete is head, head pointer is not updated, causing errors.Final Answer:
Does not handle deleting the head node correctly. -> Option AQuick Check:
Head deletion needs special handling [OK]
- Ignoring head node deletion
- Not updating head pointer
- Assuming node always exists
Solution
Step 1: Locate last node
Traverse list to find node pointing to head (value 35).Step 2: Update last node's next pointer
Set last node's next to head's next (node with value 15).Step 3: Update head pointer
Set head to head's next (15) to maintain circular list.Final Answer:
Find last node (35), set its next to head's next (15), update head to 15. -> Option DQuick Check:
Update last node and head to delete head node [OK]
- Not updating last node's next pointer
- Not updating head pointer
- Deleting head without fixing links
Ready to prove it?
Pass the test and this pattern moves unknown
