Bird
Raised Fist0
DSA Javascriptprogramming~20 mins

Delete a Node from Circular Linked List in DSA Javascript - Practice Problems & Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Circular List Deletion Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output after deleting a node with value 3
What is the printed state of the circular linked list after deleting the node with value 3?
DSA Javascript
class Node {
  constructor(data) {
    this.data = data;
    this.next = null;
  }
}

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

  append(data) {
    const newNode = new Node(data);
    if (!this.head) {
      this.head = newNode;
      newNode.next = this.head;
      return;
    }
    let temp = this.head;
    while (temp.next !== this.head) {
      temp = temp.next;
    }
    temp.next = newNode;
    newNode.next = this.head;
  }

  deleteNode(key) {
    if (!this.head) return;

    let curr = this.head, prev = null;

    // If head is to be deleted
    if (curr.data === key) {
      if (curr.next === this.head) {
        this.head = null;
        return;
      }
      let temp = this.head;
      while (temp.next !== this.head) {
        temp = temp.next;
      }
      temp.next = curr.next;
      this.head = curr.next;
      return;
    }

    prev = curr;
    curr = curr.next;
    while (curr !== this.head && curr.data !== key) {
      prev = curr;
      curr = curr.next;
    }

    if (curr.data === key) {
      prev.next = curr.next;
    }
  }

  printList() {
    if (!this.head) return "null";
    let result = "";
    let temp = this.head;
    do {
      result += temp.data + " -> ";
      temp = temp.next;
    } while (temp !== this.head);
    result += "null";
    return result;
  }
}

const cll = new CircularLinkedList();
cll.append(1);
cll.append(2);
cll.append(3);
cll.append(4);
cll.deleteNode(3);
console.log(cll.printList());
A1 -> 2 -> 4 -> null
B2 -> 4 -> 1 -> null
C1 -> 2 -> 3 -> 4 -> null
D1 -> 4 -> null
Attempts:
2 left
💡 Hint
Remember that deleting a node removes it from the circle and links the previous node to the next node.
❓ Predict Output
intermediate
2:00remaining
Output after deleting the head node
What is the printed state of the circular linked list after deleting the head node with value 1?
DSA Javascript
class Node {
  constructor(data) {
    this.data = data;
    this.next = null;
  }
}

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

  append(data) {
    const newNode = new Node(data);
    if (!this.head) {
      this.head = newNode;
      newNode.next = this.head;
      return;
    }
    let temp = this.head;
    while (temp.next !== this.head) {
      temp = temp.next;
    }
    temp.next = newNode;
    newNode.next = this.head;
  }

  deleteNode(key) {
    if (!this.head) return;

    let curr = this.head, prev = null;

    if (curr.data === key) {
      if (curr.next === this.head) {
        this.head = null;
        return;
      }
      let temp = this.head;
      while (temp.next !== this.head) {
        temp = temp.next;
      }
      temp.next = curr.next;
      this.head = curr.next;
      return;
    }

    prev = curr;
    curr = curr.next;
    while (curr !== this.head && curr.data !== key) {
      prev = curr;
      curr = curr.next;
    }

    if (curr.data === key) {
      prev.next = curr.next;
    }
  }

  printList() {
    if (!this.head) return "null";
    let result = "";
    let temp = this.head;
    do {
      result += temp.data + " -> ";
      temp = temp.next;
    } while (temp !== this.head);
    result += "null";
    return result;
  }
}

const cll = new CircularLinkedList();
cll.append(1);
cll.append(2);
cll.append(3);
cll.append(4);
cll.deleteNode(1);
console.log(cll.printList());
A2 -> 3 -> 4 -> null
B3 -> 4 -> 2 -> null
C1 -> 2 -> 3 -> 4 -> null
Dnull
Attempts:
2 left
💡 Hint
Deleting the head means the next node becomes the new head.
❓ Predict Output
advanced
2:00remaining
Output after deleting the only node in the list
What is the printed state of the circular linked list after deleting the only node with value 10?
DSA Javascript
class Node {
  constructor(data) {
    this.data = data;
    this.next = null;
  }
}

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

  append(data) {
    const newNode = new Node(data);
    if (!this.head) {
      this.head = newNode;
      newNode.next = this.head;
      return;
    }
    let temp = this.head;
    while (temp.next !== this.head) {
      temp = temp.next;
    }
    temp.next = newNode;
    newNode.next = this.head;
  }

  deleteNode(key) {
    if (!this.head) return;

    let curr = this.head, prev = null;

    if (curr.data === key) {
      if (curr.next === this.head) {
        this.head = null;
        return;
      }
      let temp = this.head;
      while (temp.next !== this.head) {
        temp = temp.next;
      }
      temp.next = curr.next;
      this.head = curr.next;
      return;
    }

    prev = curr;
    curr = curr.next;
    while (curr !== this.head && curr.data !== key) {
      prev = curr;
      curr = curr.next;
    }

    if (curr.data === key) {
      prev.next = curr.next;
    }
  }

  printList() {
    if (!this.head) return "null";
    let result = "";
    let temp = this.head;
    do {
      result += temp.data + " -> ";
      temp = temp.next;
    } while (temp !== this.head);
    result += "null";
    return result;
  }
}

const cll = new CircularLinkedList();
cll.append(10);
cll.deleteNode(10);
console.log(cll.printList());
A10 -> null
Bnull
CError: Cannot delete the only node
D10 -> 10 -> null
Attempts:
2 left
💡 Hint
Deleting the only node empties the list.
❓ Predict Output
advanced
2:00remaining
Output after deleting a non-existent node
What is the printed state of the circular linked list after trying to delete a node with value 5 which does not exist?
DSA Javascript
class Node {
  constructor(data) {
    this.data = data;
    this.next = null;
  }
}

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

  append(data) {
    const newNode = new Node(data);
    if (!this.head) {
      this.head = newNode;
      newNode.next = this.head;
      return;
    }
    let temp = this.head;
    while (temp.next !== this.head) {
      temp = temp.next;
    }
    temp.next = newNode;
    newNode.next = this.head;
  }

  deleteNode(key) {
    if (!this.head) return;

    let curr = this.head, prev = null;

    if (curr.data === key) {
      if (curr.next === this.head) {
        this.head = null;
        return;
      }
      let temp = this.head;
      while (temp.next !== this.head) {
        temp = temp.next;
      }
      temp.next = curr.next;
      this.head = curr.next;
      return;
    }

    prev = curr;
    curr = curr.next;
    while (curr !== this.head && curr.data !== key) {
      prev = curr;
      curr = curr.next;
    }

    if (curr.data === key) {
      prev.next = curr.next;
    }
  }

  printList() {
    if (!this.head) return "null";
    let result = "";
    let temp = this.head;
    do {
      result += temp.data + " -> ";
      temp = temp.next;
    } while (temp !== this.head);
    result += "null";
    return result;
  }
}

const cll = new CircularLinkedList();
cll.append(1);
cll.append(2);
cll.append(3);
cll.append(4);
cll.deleteNode(5);
console.log(cll.printList());
AError: Node not found
B2 -> 3 -> 4 -> null
C1 -> 2 -> 3 -> 4 -> null
Dnull
Attempts:
2 left
💡 Hint
If the node to delete is not found, the list remains unchanged.
🧠 Conceptual
expert
2:00remaining
Why is it necessary to update the last node's next pointer when deleting the head node in a circular linked list?
In a circular linked list, when deleting the head node, why must we update the last node's next pointer to the new head?
ABecause the last node's next pointer should point to the deleted node to keep track of it.
BBecause the last node's next pointer points to null and must be changed to avoid errors.
CBecause the last node's next pointer is irrelevant and does not affect the list structure.
DBecause the last node's next pointer must always point to the head to maintain the circular structure.
Attempts:
2 left
💡 Hint
Think about what keeps the list circular.

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