Delete a Node from Circular Linked List in DSA Java - 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.
// Delete node with given key from circular linked list
Node deleteNode(Node head, int key) {
if (head == null) return null;
Node curr = head, prev = null;
do {
if (curr.data == key) {
if (prev != null) prev.next = curr.next;
else {
// If only one node in the list
if (curr.next == head) {
return null;
}
head = curr.next;
}
break;
}
prev = curr;
curr = curr.next;
} while (curr != head);
return head;
}
This code searches the circular linked list for a node with the given key and removes it.
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 nodes to check grows too.
| 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 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 change pointers."
[OK] Correct: You must first find the node, which can take time proportional to the list size, so it is not always constant time.
Understanding this helps you explain how linked lists work and how searching affects performance, a key skill in many coding challenges.
"What if the list was doubly linked? How would the time complexity change when deleting a node?"
Practice
Solution
Step 1: Understand single node deletion
When the only node is deleted, no nodes remain in the list.Step 2: Resulting list state
The head pointer is set to null, indicating an empty list.Final Answer:
The list becomes empty (head points to null) -> Option AQuick Check:
Deleting last node empties list [OK]
- Assuming list still has nodes after deletion
- Not setting head to null
- Confusing circular with linear list after deletion
Solution
Step 1: Update head pointer
When deleting the head node, move head to next node.Step 2: Maintain circular link
Update tail's next pointer to new head to keep circular structure.Final Answer:
head = head.next; tail.next = head; -> Option DQuick Check:
Head moves forward, tail links to new head [OK]
- Setting tail.next to null breaks circular link
- Setting head to null incorrectly
- Using head.prev which doesn't exist in singly circular list
Node head = new Node(1); Node n2 = new Node(2); Node n3 = new Node(3); Node n4 = new Node(4); head.next = n2; n2.next = n3; n3.next = n4; n4.next = head; // Delete node with value 3 // After deletion, print list starting from head
Solution
Step 1: Locate node with value 3
Traverse list: 1 -> 2 -> 3 found.Step 2: Remove node 3 by linking 2 to 4
Set n2.next = n3.next (which is n4), skipping node 3.Final Answer:
1 -> 2 -> 4 -> (back to 1) -> Option AQuick Check:
Node 3 removed, list links 2 to 4 [OK]
- Not updating previous node's next pointer
- Breaking circular link by missing tail update
- Deleting wrong node or skipping nodes
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: Check tail update on deletion
If deleted node is tail, tail pointer must update to current node.Step 2: Code misses tail update
Code only changes current.next, tail remains pointing to deleted node.Final Answer:
Does not update tail pointer when deleting last node -> Option CQuick Check:
Tail pointer must update if last node deleted [OK]
- Ignoring tail pointer update
- Assuming tail auto-updates
- Not checking if deleted node is tail
Solution
Step 1: Move head to next node
Set head = head.next to remove old head from start.Step 2: Update tail's next pointer
Set tail.next = head to maintain circular link.Step 3: Delete old head node properly
Remove old head node from memory if needed.Final Answer:
Set head = head.next; tail.next = head; delete old head; -> Option BQuick Check:
Head moves forward, tail links to new head [OK]
- Breaking circular link by setting tail.next to null
- Not moving head pointer
- Deleting head without updating tail.next
Ready to prove it?
Pass the test and this pattern moves unknown
