Bird
Raised Fist0
DSA Javascriptprogramming~10 mins

Delete a Node from Circular Linked List in DSA Javascript - Execution Trace

Choose your learning style10 modes available
Concept Flow - Delete a Node from Circular Linked List
Start at head
↓
Check if list empty?
Yes→Stop: Nothing to delete
No↓
Traverse nodes until node.data == target or back to head
↓
If node found?
No→Stop: Node not found
Yes↓
If node is head and only one node
↓
Delete node, list becomes empty
↓
Else if node is head
↓
Find last node to update its next pointer
↓
Update last node's next to head.next
↓
Update head to head.next
↓
Else
↓
Update previous node's next to current.next
↓
Node deleted
↓
Stop
Start at head, traverse to find node to delete. If found, update pointers carefully to remove node while keeping circular links intact.
Execution Sample
DSA Javascript
function deleteNode(head, key) {
  if (!head) return null;
  let curr = head;
  let prev = null;
  do {
    if (curr.data === key) {
      if (curr === head) {
        if (curr.next === head) {
          return null;
        }
        let last = curr;
        while (last.next !== head) {
          last = last.next;
        }
        last.next = curr.next;
        head = curr.next;
        return head;
      } else {
        prev.next = curr.next;
        return head;
      }
    }
    prev = curr;
    curr = curr.next;
  } while (curr !== head);
  return head;
}
This code searches for the node with the given key and deletes it from the circular linked list.
Execution Table
StepOperationCurrent NodePrevious NodeNodes in ListPointer ChangesVisual State
1Start at head1null1 -> 2 -> 3 -> 4 -> (back to 1)None1 -> 2 -> 3 -> 4 -> (head)
2Check if current node data == 31null1 -> 2 -> 3 -> 4 -> (back to 1)None1 -> 2 -> 3 -> 4 -> (head)
3Move to next node211 -> 2 -> 3 -> 4 -> (back to 1)None1 -> 2 -> 3 -> 4 -> (head)
4Check if current node data == 3211 -> 2 -> 3 -> 4 -> (back to 1)None1 -> 2 -> 3 -> 4 -> (head)
5Move to next node321 -> 2 -> 3 -> 4 -> (back to 1)None1 -> 2 -> 3 -> 4 -> (head)
6Found node with data 3, delete it321 -> 2 -> 4 -> (back to 1)prev.next = curr.next (2.next = 4)1 -> 2 -> 4 -> (head)
7Stop after deletion421 -> 2 -> 4 -> (back to 1)None1 -> 2 -> 4 -> (head)
💡 Node with data 3 found and deleted; traversal stops.
Variable Tracker
VariableStartAfter Step 3After Step 5After Step 6Final
curr12344
prevnull1222
head11111
List Size44433
Key Moments - 3 Insights
Why do we use a do-while loop instead of a while loop?
Because the list is circular, we must check the node at least once before stopping. The do-while loop ensures we visit the head node and stop only after a full circle (see execution_table steps 1-5).
What happens if the node to delete is the head?
We must update the head pointer and also update the last node's next pointer to the new head to maintain circularity. This is not shown in the sample but is critical to keep the list connected.
Why do we update prev.next to curr.next when deleting?
Because we remove the current node by skipping it in the chain. Setting prev.next = curr.next bypasses the current node, effectively deleting it (see step 6 in execution_table).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the value of 'curr' at Step 5?
A1
B2
C3
D4
💡 Hint
Check the 'Current Node' column at Step 5 in the execution_table.
At which step does the list size decrease?
AStep 4
BStep 6
CStep 7
DStep 3
💡 Hint
Look at the 'List Size' row in variable_tracker and see when it changes.
If the node to delete was the head, what extra pointer update is needed?
AUpdate head to head.next and last node's next to new head
BOnly update head to head.next
COnly update last node's next to head.next
DNo pointer update needed
💡 Hint
Refer to key_moments about deleting the head node.
Concept Snapshot
Delete Node in Circular Linked List:
- Start at head, traverse nodes with do-while
- Find node with target data
- If node is head, update head and last node's next
- Else, update prev.next to curr.next
- Maintain circular link after deletion
- Return updated head
Full Transcript
To delete a node from a circular linked list, start at the head node and traverse the list using a do-while loop to ensure all nodes are checked. When the node with the target value is found, if it is the head node, update the head pointer and also update the last node's next pointer to the new head to keep the list circular. If the node is not the head, update the previous node's next pointer to skip the current node, effectively deleting it. The traversal stops once the node is deleted or the entire list is checked. This process maintains the circular structure of the list after deletion.

Practice

(1/5)
1. What happens when you delete the head node from a circular linked list with multiple nodes?
easy
A. The head moves to the next node and the list remains circular
B. The list becomes empty immediately
C. The last node points to null breaking the circle
D. The list throws an error and stops

Solution

  1. Step 1: Understand the head deletion in circular list

    When the head node is deleted, the next node becomes the new head.
  2. Step 2: Maintain circular link

    The last node's next pointer is updated to point to the new head, keeping the list circular.
  3. Final Answer:

    The head moves to the next node and the list remains circular -> Option A
  4. Quick Check:

    Head deletion updates head and last node pointer [OK]
Hint: Head deletion shifts head and keeps circle intact [OK]
Common Mistakes:
  • 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
2. Which of the following is the correct syntax to delete a node with value `key` from a circular linked list in JavaScript?
easy
A. function deleteNode(head) { /* code */ }
B. function deleteNode(key, head) { /* code */ }
C. function deleteNode(head, key) { /* code */ }
D. function deleteNode() { /* code */ }

Solution

  1. Step 1: Identify function parameters for deletion

    The function needs the head of the list and the key value to delete the correct node.
  2. Step 2: Confirm correct parameter order

    The common and clear order is (head, key) to access the list and the target value.
  3. Final Answer:

    function deleteNode(head, key) { /* code */ } -> Option C
  4. Quick Check:

    Parameters: head and key in order [OK]
Hint: Function needs head and key parameters in order [OK]
Common Mistakes:
  • Swapping parameter order causing confusion
  • Missing key parameter to identify node
  • Using no parameters making deletion impossible
3. What will be the output after deleting node with value 3 from this circular linked list: 1 -> 3 -> 5 -> 7 -> (back to 1)?
<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
medium
A. 1 -> 5 -> 7 -> (back to 1)
B. 3 -> 5 -> 7 -> (back to 3)
C. 1 -> 3 -> 5 -> 7 -> (back to 1)
D. 5 -> 7 -> 1 -> (back to 5)

Solution

  1. Step 1: Locate node with value 3

    The code loops through nodes and finds node with data 3.
  2. 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.
  3. Final Answer:

    1 -> 5 -> 7 -> (back to 1) -> Option A
  4. Quick Check:

    Node 3 removed, list links updated [OK]
Hint: Skip deleted node by linking previous to next [OK]
Common Mistakes:
  • Not updating previous node's next pointer
  • Deleting head incorrectly when key is not head
  • Breaking circular link by missing last node update
4. Identify the bug in this code snippet for deleting a node from a circular linked list:
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;
}
medium
A. Infinite loop due to missing break
B. Does not handle deletion when head node has the key
C. Deletes multiple nodes with the key instead of one
D. Returns null even if list is not empty

Solution

  1. Step 1: Analyze loop and deletion logic

    The loop checks curr.next for key but never checks head node itself.
  2. Step 2: Identify missing head deletion case

    If head node contains key, this code does not delete it, causing incorrect behavior.
  3. Final Answer:

    Does not handle deletion when head node has the key -> Option B
  4. Quick Check:

    Head node deletion missing in logic [OK]
Hint: Always check head node separately for deletion [OK]
Common Mistakes:
  • Ignoring head node deletion case
  • Assuming loop covers all nodes including head
  • Not updating head pointer when head is deleted
5. Given a circular linked list with nodes 10 -> 20 -> 30 -> 40 -> (back to 10), how would you delete the last node (40) and maintain the circular structure? Choose the correct approach.
hard
A. Swap last node data with head and delete head
B. Set head to null directly to delete last node
C. Change last node's next to null to break the circle
D. Traverse to node before last, set its next to head, and delete last node

Solution

  1. Step 1: Find node before last node

    Traverse list until node.next.next equals head, identifying node before last.
  2. 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.
  3. Final Answer:

    Traverse to node before last, set its next to head, and delete last node -> Option D
  4. Quick Check:

    Update second last node's next to head [OK]
Hint: Link second last node directly to head [OK]
Common Mistakes:
  • Breaking circle by setting next to null
  • Deleting head instead of last node
  • Not updating pointers causing broken list
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard