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.
public void DeleteNode(int key) {
if (head == null) return;
Node current = head, prev = null;
do {
if (current.data == key) {
if (prev != null) prev.next = current.next;
else {
// If the list has only one node
if (current.next == head) {
head = null;
} else {
// Find the last node to update its next pointer
Node last = head;
while (last.next != head) {
last = last.next;
}
head = current.next;
last.next = head;
}
}
return;
}
prev = current;
current = current.next;
} while (current != 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.
| 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 directly with the list size.
[X] Wrong: "Deleting a node is always constant time because we just change pointers."
[OK] Correct: We must first find the node, which can take time proportional to the list size.
Understanding this helps you explain how linked lists work and how operations depend on list size, a key skill in interviews.
"What if the list kept a direct reference to the node to delete? How would the time complexity change?"
Practice
Solution
Step 1: Understand the structure of a single-node circular linked list
In a circular linked list with one node, the node points to itself.Step 2: Delete the only node
Removing this node leaves no nodes, so the list becomes empty (null).Final Answer:
The list becomes empty (null). -> Option CQuick Check:
Deleting the only node empties the list [OK]
- Assuming list still has one node after deletion
- Thinking list becomes linear instead of empty
- Expecting an error on deletion
Solution
Step 1: Identify the new head after deleting the current head
When deleting the head node, the new head should be the next node in the list.Step 2: Update the head pointer
Assign head to head.next to point to the next node.Final Answer:
head = head.next; -> Option AQuick Check:
New head is next node after deletion [OK]
- Setting head to null incorrectly
- Using head.prev which is invalid in singly circular list
- Skipping updating head pointer
Node head = new Node(1); head.next = new Node(2); head.next.next = new Node(3); head.next.next.next = new Node(4); head.next.next.next.next = head; // Delete node with value 3 // After deletion, print list starting from head
Solution
Step 1: Identify node to delete and its previous node
Node with value 3 is after node with value 2.Step 2: Update previous node's next pointer
Set node 2's next to node 4, skipping node 3.Final Answer:
1 -> 2 -> 4 -> (back to 1) -> Option DQuick Check:
Node 3 removed, list links 2 to 4 [OK]
- Not updating previous node's next pointer
- Deleting wrong node
- Breaking circular link
Node current = head;
while (current.next != head && current.next.data != key) {
current = current.next;
}
if (current.next.data == key) {
current.next = current.next.next;
}Solution
Step 1: Analyze deletion logic
The code finds the node before the target and skips the target node.Step 2: Check head pointer update
If the deleted node is head, head pointer is not updated, causing inconsistency.Final Answer:
Does not update head pointer after deletion. -> Option BQuick Check:
Head pointer must update if head node deleted [OK]
- Ignoring head pointer update
- Assuming deletion always non-head
- Not checking if key exists
Solution
Step 1: Find the last node in the circular list
Traverse nodes until node.next == head to find last node (node with value 40).Step 2: Update last node's next pointer and head
Set last node's next to head.next (node 20), then update head to head.next.Final Answer:
Find last node, set its next to head.next, update head to head.next. -> Option AQuick Check:
Last node points to new head, list remains circular [OK]
- Not updating last node's next pointer
- Setting head to null incorrectly
- Breaking circular link by pointing to null
Ready to prove it?
Pass the test and this pattern moves unknown
