Bird
Raised Fist0
DSA Javascriptprogramming

Delete a Node from Circular Linked List in DSA Javascript

Choose your learning style10 modes available
Mental Model
A circular linked list loops back to the start, so deleting a node means adjusting pointers to skip that node without breaking the loop.
Analogy: Imagine people standing in a circle holding hands. To remove one person, the two neighbors hold hands directly, keeping the circle unbroken.
head -> 1 -> 2 -> 3 -> 4 -> back to head (1)
(arrow loops from 4 back to 1)
Dry Run Walkthrough
Input: list: 1 -> 2 -> 3 -> 4 (circular), delete node with value 3
Goal: Remove node with value 3 and keep the list circular
Step 1: start at head node with value 1
head -> [1] -> 2 -> 3 -> 4 -> back to head (1)
Why: We begin searching from the head to find the node to delete
Step 2: move to next node with value 2
head -> 1 -> [2] -> 3 -> 4 -> back to head (1)
Why: Check next node to see if it is the one to delete
Step 3: next node is 3, which matches value to delete
head -> 1 -> 2 -> [3] -> 4 -> back to head (1)
Why: Found the node to delete, need to adjust pointers
Step 4: set node 2's next pointer to node 4, skipping node 3
head -> 1 -> 2 -> 4 -> back to head (1)
Why: By skipping node 3, we remove it from the circle
Result:
head -> 1 -> 2 -> 4 -> back to head (1)
Annotated Code
DSA Javascript
class Node {
  constructor(value) {
    this.value = value;
    this.next = null;
  }
}

class CircularLinkedList {
  constructor() {
    this.head = null;
  }

  insert(value) {
    const newNode = new Node(value);
    if (!this.head) {
      newNode.next = newNode;
      this.head = newNode;
      return;
    }
    let curr = this.head;
    while (curr.next !== this.head) {
      curr = curr.next;
    }
    curr.next = newNode;
    newNode.next = this.head;
  }

  delete(value) {
    if (!this.head) return; // empty list

    let curr = this.head;
    let prev = null;

    // special case: head is the node to delete
    if (curr.value === value) {
      if (curr.next === this.head) {
        // only one node
        this.head = null;
        return;
      }
      // find last node to update its next pointer
      let last = this.head;
      while (last.next !== this.head) {
        last = last.next;
      }
      last.next = curr.next;
      this.head = curr.next;
      return;
    }

    prev = curr;
    curr = curr.next;

    while (curr !== this.head) {
      if (curr.value === value) {
        prev.next = curr.next; // skip the node to delete
        return;
      }
      prev = curr;
      curr = curr.next;
    }
  }

  print() {
    if (!this.head) {
      console.log('empty list');
      return;
    }
    let curr = this.head;
    let result = '';
    do {
      result += curr.value + ' -> ';
      curr = curr.next;
    } while (curr !== this.head);
    result += '(back to head)';
    console.log(result);
  }
}

// driver code
const cll = new CircularLinkedList();
cll.insert(1);
cll.insert(2);
cll.insert(3);
cll.insert(4);
console.log('Original list:');
cll.print();

cll.delete(3);
console.log('After deleting 3:');
cll.print();
if (!this.head) return; // empty list
handle empty list case early
if (curr.value === value) { ... }
handle deletion if head node matches value
while (last.next !== this.head) { last = last.next; }
find last node to update its next pointer when deleting head
prev.next = curr.next; // skip the node to delete
bypass the node to delete by linking previous node to next node
do { result += curr.value + ' -> '; curr = curr.next; } while (curr !== this.head);
traverse circular list once to print all nodes
OutputSuccess
Original list: 1 -> 2 -> 3 -> 4 -> (back to head) After deleting 3: 1 -> 2 -> 4 -> (back to head)
Complexity Analysis
Time: O(n) because in worst case we traverse all nodes once to find the node to delete
Space: O(1) because we only use a few pointers regardless of list size
vs Alternative: Compared to a singly linked list, circular list requires extra steps to maintain the loop, but deletion is still O(n) in worst case
Edge Cases
empty list
delete does nothing, list remains empty
DSA Javascript
if (!this.head) return; // empty list
single node list where node to delete is head
list becomes empty after deletion
DSA Javascript
if (curr.next === this.head) { this.head = null; return; }
node to delete is head but list has multiple nodes
head moves to next node, last node points to new head
DSA Javascript
last.next = curr.next; this.head = curr.next;
node to delete not found
list remains unchanged
DSA Javascript
while (curr !== this.head) { if (curr.value === value) { ... } }
When to Use This Pattern
When you see a problem asking to remove an element from a circular linked list, use pointer adjustment to skip the node while keeping the loop intact.
Common Mistakes
Mistake: Not updating the last node's next pointer when deleting the head node
Fix: Find the last node and update its next pointer to the new head
Mistake: Breaking the circular link by setting a node's next to null
Fix: Always keep next pointers pointing to a node in the list to maintain the circle
Mistake: Infinite loop by not checking when traversal returns to head
Fix: Use a condition to stop traversal when current node equals head again
Summary
Deletes a node from a circular linked list by adjusting pointers to maintain the loop.
Use when you need to remove an element from a circular linked list without breaking its circular nature.
The key is to carefully update the previous node's next pointer and handle the special case when deleting the head.

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