Delete a Node from Circular Linked List in DSA Go - 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.
func deleteNode(head *Node, key int) *Node {
if head == nil {
return nil
}
curr := head
var prev *Node = nil
for {
if curr.data == key {
if prev != nil {
prev.next = curr.next
} else {
// handle deleting head
// find last node to update its next pointer
last := head
for last.next != head {
last = last.next
}
if curr.next == head { // only one node
return nil
}
last.next = curr.next
head = curr.next
}
break
}
prev = curr
curr = curr.next
if curr == head {
break
}
}
return head
}
This code searches the circular linked list to find a node with the given key and deletes 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 steps to find the node grows roughly the same as the list size.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | Up to 10 steps |
| 100 | Up to 100 steps |
| 1000 | Up to 1000 steps |
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 because we may need to check each node once.
[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 code efficiently.
"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 means no nodes remain, so head becomes nil.Final Answer:
The list becomes empty (head points to nil). -> Option BQuick Check:
Single node deleted = empty list [OK]
- Assuming list still has nodes after deleting the only node
- Not updating head to nil
- Confusing circular with linear list after deletion
Solution
Step 1: Identify head deletion logic
Deleting head means moving head pointer to next node.Step 2: Check syntax correctness
Assigning head = head.next correctly moves head to next node.Final Answer:
head = head.next -> Option CQuick Check:
Move head to next node = head = head.next [OK]
- Using nil.next causes runtime error
- Assigning head.next = head does not delete node
- Using head.prev is invalid for singly circular list
Solution
Step 1: Locate node with value 2
Node 2 is between nodes 1 and 3 in the circular list.Step 2: Remove node 2 by linking 1 to 3
Update node 1's next pointer to node 3, skipping node 2.Final Answer:
1 -> 3 -> (back to 1) -> Option AQuick Check:
Deleted 2, linked 1 to 3 [OK]
- Not updating previous node's next pointer
- Deleting wrong node
- Breaking circular link
func deleteNode(head *Node, key int) *Node {
if head == nil {
return nil
}
curr := head
for curr.next != head && curr.next.data != key {
curr = curr.next
}
if curr.next.data == key {
curr.next = curr.next.next
}
return head
}Solution
Step 1: Analyze deletion logic
The code searches for node before target and skips target node.Step 2: Check head deletion case
If the node to delete is head, head pointer is not updated, causing incorrect list.Final Answer:
It does not handle deleting the head node correctly. -> Option DQuick Check:
Head deletion needs head pointer update [OK]
- Ignoring head node deletion
- Not updating head pointer
- Assuming key always exists
Solution
Step 1: Find last node pointing to head
Traverse list to find node 40 which points to head (10).Step 2: Update last node's next pointer
Set node 40's next to head.next (node 20) to maintain circularity.Step 3: Update head pointer
Set head to head.next (node 20) to delete original head.Final Answer:
Find last node (40), set its next to head.next (20), then update head to 20. -> Option AQuick Check:
Update last node and head to delete head node [OK]
- Not updating last node's next pointer
- Setting head to nil breaks list
- Deleting node by setting next to nil
Ready to prove it?
Pass the test and this pattern moves unknown
