Challenge - 5 Problems
Circular List Deletion Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput 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());
Attempts:
2 left
💡 Hint
Remember that deleting a node removes it from the circle and links the previous node to the next node.
✗ Incorrect
The node with value 3 is removed. The list links 2 directly to 4, maintaining the circular structure. The printed output shows the list starting from head 1, then 2, then 4, then null.
❓ Predict Output
intermediateOutput 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());
Attempts:
2 left
💡 Hint
Deleting the head means the next node becomes the new head.
✗ Incorrect
The head node with value 1 is deleted. The last node (4) now points to the new head (2). The list prints starting from 2, then 3, then 4, then null.
❓ Predict Output
advancedOutput 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());
Attempts:
2 left
💡 Hint
Deleting the only node empties the list.
✗ Incorrect
After deleting the only node, the list becomes empty. The printList method returns 'null'.
❓ Predict Output
advancedOutput 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());
Attempts:
2 left
💡 Hint
If the node to delete is not found, the list remains unchanged.
✗ Incorrect
Since the node with value 5 does not exist, the list remains unchanged and prints all nodes.
🧠 Conceptual
expertWhy 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?
Attempts:
2 left
💡 Hint
Think about what keeps the list circular.
✗ Incorrect
In a circular linked list, the last node's next pointer always points to the head node. When the head is deleted, the last node must point to the new head to keep the list circular.
