Challenge - 5 Problems
Circular Singly Linked List Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of Circular Singly Linked List after Insertions
What is the printed state of the circular singly linked list after inserting nodes with values 10, 20, and 30 in that order?
DSA Typescript
class Node { value: number; next: Node | null; constructor(value: number) { this.value = value; this.next = null; } } class CircularSinglyLinkedList { head: Node | null = null; insert(value: number) { const newNode = new Node(value); if (!this.head) { this.head = newNode; newNode.next = this.head; } else { let temp = this.head; while (temp.next !== this.head) { temp = temp.next!; } temp.next = newNode; newNode.next = this.head; } } printList(): string { if (!this.head) return "null"; let result = ""; let temp = this.head; do { result += temp.value + " -> "; temp = temp.next!; } while (temp !== this.head); result += "(head)"; return result; } } const list = new CircularSinglyLinkedList(); list.insert(10); list.insert(20); list.insert(30); console.log(list.printList());
Attempts:
2 left
💡 Hint
Remember that in a circular singly linked list, the last node points back to the head node.
✗ Incorrect
The insert method adds nodes at the end. After inserting 10, 20, and 30, the list is 10 -> 20 -> 30 and the last node points back to the head (10). So the printed output ends with (head).
❓ Predict Output
intermediateOutput after Deleting Head Node in Circular Singly Linked List
Given a circular singly linked list with nodes 5 -> 15 -> 25 -> (head), what is the printed state after deleting the head node (value 5)?
DSA Typescript
class Node { value: number; next: Node | null; constructor(value: number) { this.value = value; this.next = null; } } class CircularSinglyLinkedList { head: Node | null = null; insert(value: number) { const newNode = new Node(value); if (!this.head) { this.head = newNode; newNode.next = this.head; } else { let temp = this.head; while (temp.next !== this.head) { temp = temp.next!; } temp.next = newNode; newNode.next = this.head; } } deleteHead() { if (!this.head) return; if (this.head.next === this.head) { this.head = null; return; } let temp = this.head; while (temp.next !== this.head) { temp = temp.next!; } temp.next = this.head.next; this.head = this.head.next; } printList(): string { if (!this.head) return "null"; let result = ""; let temp = this.head; do { result += temp.value + " -> "; temp = temp.next!; } while (temp !== this.head); result += "(head)"; return result; } } const list = new CircularSinglyLinkedList(); list.insert(5); list.insert(15); list.insert(25); list.deleteHead(); console.log(list.printList());
Attempts:
2 left
💡 Hint
Deleting the head means the second node becomes the new head and the last node points to it.
✗ Incorrect
After deleting the head (5), the new head is 15. The last node (25) points to 15, so the list is 15 -> 25 -> (head).
🔧 Debug
advancedIdentify the Bug in Circular Singly Linked List Insertion
What error will occur when running this insertion method in a circular singly linked list?
DSA Typescript
insert(value: number) {
const newNode = new Node(value);
if (!this.head) {
this.head = newNode;
newNode.next = this.head;
} else {
let temp = this.head;
while (temp.next !== this.head) {
temp = temp.next!;
}
temp.next = newNode;
newNode.next = this.head;
}
}Attempts:
2 left
💡 Hint
In a circular list, the last node's next points to head, never null.
✗ Incorrect
The while loop condition checks for temp.next !== null, but in a circular list temp.next never becomes null, causing an infinite loop.
🚀 Application
advancedFind Length of Circular Singly Linked List
Which code snippet correctly calculates the number of nodes in a circular singly linked list?
Attempts:
2 left
💡 Hint
Use a do-while loop to ensure the head node is counted and stop when you reach head again.
✗ Incorrect
Option A correctly uses a do-while loop to count nodes until it loops back to head. Other options either check for null (which never occurs) or miss counting the last node.
🧠 Conceptual
expertWhy Use Circular Singly Linked List Over Singly Linked List?
Which is the main advantage of using a circular singly linked list instead of a regular singly linked list?
Attempts:
2 left
💡 Hint
Think about what happens when you reach the end of the list in a circular structure.
✗ Incorrect
In a circular singly linked list, the last node points back to the head, allowing continuous traversal without encountering null, unlike a regular singly linked list.
