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 == NULL) return;
Node *curr = *head, *prev = NULL;
do {
if (curr->data == key) {
if (prev != NULL) 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;
}
free(curr);
return;
}
prev = curr;
curr = curr->next;
} while (curr != *head);
}
This code searches for a node with a given value and deletes it from the circular linked list.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Traversing the circular linked list nodes one by one.
- How many times: Up to n times, where n is the number of nodes in the list.
As the list grows, the number of nodes to check grows too.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | Up to 10 node checks |
| 100 | Up to 100 node checks |
| 1000 | Up to 1000 node checks |
Pattern observation: The steps grow roughly in direct proportion to the list size.
Time Complexity: O(n)
This means the time to delete a node grows linearly with the number of nodes in the list.
[X] Wrong: "Deleting a node in a circular linked list always takes constant time because we just remove it."
[OK] Correct: We must first find the node, which can take time proportional to the list size, so deletion is not always instant.
Understanding this helps you explain how linked list operations scale and shows you can analyze real code efficiently.
"What if the list had a pointer to the node before the one to delete? How would the time complexity change?"
Practice
Solution
Step 1: Understand the structure of a single-node circular linked list
It has one node whose next pointer points to itself.Step 2: Delete the only node
After deletion, no nodes remain, so the head pointer must be set to NULL to indicate an empty list.Final Answer:
The list becomes empty (head = NULL) -> Option AQuick Check:
Single node deleted means empty list [OK]
- Not setting head to NULL after deleting the only node
- Assuming list still has one node after deletion
- Confusing circular list with linear list after deletion
Solution
Step 1: Understand deletion of head node in circular linked list
We must move head pointer to next node and free the old head node.Step 2: Check syntax correctness
Node* oldHead = head; head = head->next; free(oldHead); correctly assigns head to head->next and frees the old head pointer stored separately.Final Answer:
Node* oldHead = head; head = head->next; free(oldHead); -> Option DQuick Check:
Move head, free old node [OK]
- Freeing head before moving pointer
- Setting next pointer to NULL breaks circular link
- Freeing NULL pointer
Solution
Step 1: Identify node to delete and its neighbors
Node with value 20 is between 10 and 30.Step 2: Adjust pointers to remove node 20
Set 10's next pointer to 30, skipping 20, maintaining circular link.Final Answer:
10 -> 30 -> (back to 10) -> Option CQuick Check:
Removed 20, linked 10 to 30 [OK]
- Not updating previous node's next pointer
- Breaking circular link by setting next to NULL
- Deleting wrong node
void deleteNode(Node** head, int key) {
Node* curr = *head;
Node* prev = NULL;
while (curr->data != key) {
prev = curr;
curr = curr->next;
}
prev->next = curr->next;
free(curr);
}Solution
Step 1: Analyze deletion logic for head node
If the node to delete is head, prev is NULL, so prev->next causes error.Step 2: Identify missing head update
Code does not update *head when deleting head node, causing incorrect list state.Final Answer:
It does not handle deleting the head node correctly -> Option AQuick Check:
Head deletion needs special handling [OK]
- Not updating head pointer when deleting head
- Dereferencing NULL prev pointer
- Not checking if node exists before deletion
Solution
Step 1: Locate last node in circular list
Traverse nodes until node whose next points to head is found (node with value 20).Step 2: Update last node's next pointer and head
Set last node's next to head->next (node with value 10), free old head, and update head pointer to head->next.Final Answer:
Find last node, set its next to head->next, free head, update head to head->next -> Option BQuick Check:
Last node points to new head after deletion [OK]
- Not updating last node's next pointer
- Freeing head before updating pointers
- Breaking circular link by setting next to NULL
Ready to prove it?
Pass the test and this pattern moves unknown
