Delete a Node from Circular Linked List in DSA Javascript - 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 a node?
Analyze the time complexity of the following code snippet.
function deleteNode(head, key) {
if (!head) return null;
let curr = head, prev = null;
do {
if (curr.data === key) {
if (prev) prev.next = curr.next;
else {
// find last node to update its next
let last = head;
while (last.next !== head) last = last.next;
if (curr.next === head) return null; // only one node
last.next = curr.next;
head = curr.next;
}
break;
}
prev = curr;
curr = curr.next;
} while (curr !== head);
return head;
}
This code searches for a node with a given value and removes it from the circular linked list.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Looping through nodes to find the node to delete.
- 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 linearly.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | Up to 10 checks |
| 100 | Up to 100 checks |
| 1000 | Up to 1000 checks |
Pattern observation: The steps increase directly with the number of nodes.
Time Complexity: O(n)
This means the time to delete a node grows linearly with the list size.
[X] Wrong: "Deleting a node is always O(1) because we just change pointers."
[OK] Correct: We 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 tasks clearly.
"What if the list was doubly linked? How would the time complexity of deletion change?"
Practice
Solution
Step 1: Understand the head deletion in circular list
When the head node is deleted, the next node becomes the new head.Step 2: Maintain circular link
The last node's next pointer is updated to point to the new head, keeping the list circular.Final Answer:
The head moves to the next node and the list remains circular -> Option AQuick Check:
Head deletion updates head and last node pointer [OK]
- Assuming list becomes empty after head deletion
- Breaking the circular link by setting last node to null
- Not updating the last node's next pointer
Solution
Step 1: Identify function parameters for deletion
The function needs the head of the list and the key value to delete the correct node.Step 2: Confirm correct parameter order
The common and clear order is (head, key) to access the list and the target value.Final Answer:
function deleteNode(head, key) { /* code */ } -> Option CQuick Check:
Parameters: head and key in order [OK]
- Swapping parameter order causing confusion
- Missing key parameter to identify node
- Using no parameters making deletion impossible
<pre>function deleteNode(head, key) { if (!head) return null; let curr = head, prev = null; do { if (curr.data === key) { if (prev) prev.next = curr.next; else { let temp = head; while (temp.next !== head) temp = temp.next; head = curr.next; temp.next = head; } break; } prev = curr; curr = curr.next; } while (curr !== head); return head; } // After deletion, print list starting from head
Solution
Step 1: Locate node with value 3
The code loops through nodes and finds node with data 3.Step 2: Delete node 3 and update links
Since node 3 is not head, prev.next skips node 3 and points to node 5, maintaining circular link.Final Answer:
1 -> 5 -> 7 -> (back to 1) -> Option AQuick Check:
Node 3 removed, list links updated [OK]
- Not updating previous node's next pointer
- Deleting head incorrectly when key is not head
- Breaking circular link by missing last node update
function deleteNode(head, key) {
if (!head) return null;
let curr = head;
while (curr.next !== head) {
if (curr.next.data === key) {
curr.next = curr.next.next;
return head;
}
curr = curr.next;
}
return head;
}Solution
Step 1: Analyze loop and deletion logic
The loop checks curr.next for key but never checks head node itself.Step 2: Identify missing head deletion case
If head node contains key, this code does not delete it, causing incorrect behavior.Final Answer:
Does not handle deletion when head node has the key -> Option BQuick Check:
Head node deletion missing in logic [OK]
- Ignoring head node deletion case
- Assuming loop covers all nodes including head
- Not updating head pointer when head is deleted
Solution
Step 1: Find node before last node
Traverse list until node.next.next equals head, identifying node before last.Step 2: Update next pointer and delete last node
Set node before last's next pointer to head, removing last node and keeping list circular.Final Answer:
Traverse to node before last, set its next to head, and delete last node -> Option DQuick Check:
Update second last node's next to head [OK]
- Breaking circle by setting next to null
- Deleting head instead of last node
- Not updating pointers causing broken list
Ready to prove it?
Pass the test and this pattern moves unknown
