Challenge - 5 Problems
Circular Singly Linked List Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateWhat is the output of this code creating a circular singly linked list?
Consider the following JavaScript code that creates a circular singly linked list with three nodes. What will be printed when we traverse the list once starting from the head?
DSA Javascript
class Node { constructor(value) { this.value = value; this.next = null; } } class CircularSinglyLinkedList { constructor() { this.head = null; } append(value) { const newNode = new Node(value); if (!this.head) { this.head = newNode; newNode.next = this.head; } else { let current = this.head; while (current.next !== this.head) { current = current.next; } current.next = newNode; newNode.next = this.head; } } traverse() { let result = []; if (!this.head) return result; let current = this.head; do { result.push(current.value); current = current.next; } while (current !== this.head); return result; } } const list = new CircularSinglyLinkedList(); list.append(10); list.append(20); list.append(30); console.log(list.traverse());
Attempts:
2 left
💡 Hint
Remember that in a circular singly linked list, the last node points back to the head, so traversal stops when we reach the head again.
✗ Incorrect
The traverse method collects values starting from head and stops when it reaches head again after a full cycle. Since we added 3 nodes with values 10, 20, and 30, the output is [10, 20, 30]. Option C includes an extra 10 at the end which is incorrect because traversal stops before repeating the head.
🧠 Conceptual
intermediateHow many nodes are in the circular singly linked list after these operations?
Starting with an empty circular singly linked list, we perform these operations:
1. Append 5
2. Append 15
3. Append 25
4. Append 35
5. Remove the node with value 15
How many nodes remain in the list?
Attempts:
2 left
💡 Hint
Each append adds one node. Removing one node reduces the count by one.
✗ Incorrect
We start with 0 nodes. Append 5, 15, 25, 35 adds 4 nodes. Removing the node with value 15 leaves 3 nodes.
🔧 Debug
advancedWhat error occurs when running this circular singly linked list code?
This code tries to append nodes to a circular singly linked list but causes an error. What error will it raise?
DSA Javascript
class Node { constructor(value) { this.value = value; this.next = null; } } class CircularSinglyLinkedList { constructor() { this.head = null; } append(value) { const newNode = new Node(value); if (!this.head) { this.head = newNode; newNode.next = this.head; } else { let current = this.head; while (current.next !== null) { current = current.next; } current.next = newNode; newNode.next = this.head; } } } const list = new CircularSinglyLinkedList(); list.append(1); list.append(2); list.append(3);
Attempts:
2 left
💡 Hint
Check the while loop condition carefully. What happens when current.next never becomes null?
✗ Incorrect
The while loop condition checks for current.next !== null, but in a circular list current.next never becomes null because the last node points back to head. This causes an infinite loop.
📝 Syntax
advancedWhich option correctly implements the append method for a circular singly linked list?
Choose the correct append method code snippet that adds a new node to the end of a circular singly linked list.
Attempts:
2 left
💡 Hint
Remember to update the new node's next pointer before linking it to the current last node.
✗ Incorrect
Option A correctly sets newNode.next to current.next (which is head) before linking current.next to newNode, maintaining the circular link. Option A sets newNode.next after linking current.next, which also works but option A is the standard safe order. Option A uses null which breaks circularity. Option A sets newNode.next after the if-else block, which is correct but option A is clearer and safer.
🚀 Application
expertWhat is the output after these operations on a circular singly linked list?
Given an empty circular singly linked list, perform these operations:
1. Append 100
2. Append 200
3. Append 300
4. Remove the head node
5. Traverse the list
What is the output array after traversal?
DSA Javascript
class Node { constructor(value) { this.value = value; this.next = null; } } class CircularSinglyLinkedList { constructor() { this.head = null; } append(value) { const newNode = new Node(value); if (!this.head) { this.head = newNode; newNode.next = this.head; } else { let current = this.head; while (current.next !== this.head) { current = current.next; } current.next = newNode; newNode.next = this.head; } } removeHead() { if (!this.head) return; if (this.head.next === this.head) { this.head = null; return; } let current = this.head; while (current.next !== this.head) { current = current.next; } current.next = this.head.next; this.head = this.head.next; } traverse() { let result = []; if (!this.head) return result; let current = this.head; do { result.push(current.value); current = current.next; } while (current !== this.head); return result; } } const list = new CircularSinglyLinkedList(); list.append(100); list.append(200); list.append(300); list.removeHead(); console.log(list.traverse());
Attempts:
2 left
💡 Hint
Removing the head means the second node becomes the new head and the last node points to it.
✗ Incorrect
After appending 100, 200, 300, the list is 100 -> 200 -> 300 -> back to 100. Removing the head (100) updates head to 200 and last node (300) points to 200. Traversal outputs [200, 300].
