Challenge - 5 Problems
Circular Linked List Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateWhat is the output after inserting 10 at the beginning?
Given a circular linked list with nodes 20 -> 30 -> 40 -> back to 20, what is the printed list after inserting 10 at the beginning?
DSA Javascript
class Node { constructor(data) { this.data = data; this.next = null; } } class CircularLinkedList { constructor() { this.head = null; } insertAtBeginning(data) { const newNode = new Node(data); if (!this.head) { newNode.next = newNode; this.head = newNode; } else { let temp = this.head; while (temp.next !== this.head) { temp = temp.next; } newNode.next = this.head; temp.next = newNode; this.head = newNode; } } printList() { if (!this.head) return ''; let result = ''; let temp = this.head; do { result += temp.data + ' -> '; temp = temp.next; } while (temp !== this.head); result += 'back to ' + this.head.data; return result; } } const cll = new CircularLinkedList(); cll.insertAtBeginning(40); cll.insertAtBeginning(30); cll.insertAtBeginning(20); cll.insertAtBeginning(10); console.log(cll.printList());
Attempts:
2 left
💡 Hint
Remember that inserting at the beginning updates the head and the last node's next pointer.
✗ Incorrect
Inserting 10 at the beginning means 10 becomes the new head. The last node (which was 40) points to the new head 10. The order is 10 -> 20 -> 30 -> 40 -> back to 10.
❓ Predict Output
intermediateWhat is the output after inserting into an empty circular linked list?
What will be printed after inserting 5 into an empty circular linked list?
DSA Javascript
class Node { constructor(data) { this.data = data; this.next = null; } } class CircularLinkedList { constructor() { this.head = null; } insertAtBeginning(data) { const newNode = new Node(data); if (!this.head) { newNode.next = newNode; this.head = newNode; } else { let temp = this.head; while (temp.next !== this.head) { temp = temp.next; } newNode.next = this.head; temp.next = newNode; this.head = newNode; } } printList() { if (!this.head) return ''; let result = ''; let temp = this.head; do { result += temp.data + ' -> '; temp = temp.next; } while (temp !== this.head); result += 'back to ' + this.head.data; return result; } } const cll = new CircularLinkedList(); cll.insertAtBeginning(5); console.log(cll.printList());
Attempts:
2 left
💡 Hint
In a circular linked list with one node, the node points to itself.
✗ Incorrect
When the list has only one node, it points to itself, so the printed output is '5 -> back to 5'.
❓ Predict Output
advancedWhat is the output after multiple insertions at beginning?
Starting with an empty circular linked list, insert 1, then 2, then 3 at the beginning. What is the printed list?
DSA Javascript
class Node { constructor(data) { this.data = data; this.next = null; } } class CircularLinkedList { constructor() { this.head = null; } insertAtBeginning(data) { const newNode = new Node(data); if (!this.head) { newNode.next = newNode; this.head = newNode; } else { let temp = this.head; while (temp.next !== this.head) { temp = temp.next; } newNode.next = this.head; temp.next = newNode; this.head = newNode; } } printList() { if (!this.head) return ''; let result = ''; let temp = this.head; do { result += temp.data + ' -> '; temp = temp.next; } while (temp !== this.head); result += 'back to ' + this.head.data; return result; } } const cll = new CircularLinkedList(); cll.insertAtBeginning(1); cll.insertAtBeginning(2); cll.insertAtBeginning(3); console.log(cll.printList());
Attempts:
2 left
💡 Hint
Each new insertion at beginning becomes the new head.
✗ Incorrect
Inserting 3 at beginning makes it head, then 2 before 3, then 1 before 2. The order is 3 -> 2 -> 1 -> back to 3.
❓ Predict Output
advancedWhat error occurs if we forget to update last node's next pointer?
In the insertAtBeginning method, if we do not update the last node's next pointer to the new head, what will happen when printing the list?
DSA Javascript
class Node { constructor(data) { this.data = data; this.next = null; } } class CircularLinkedList { constructor() { this.head = null; } insertAtBeginning(data) { const newNode = new Node(data); if (!this.head) { newNode.next = newNode; this.head = newNode; } else { // Missing update of last node's next pointer newNode.next = this.head; this.head = newNode; } } printList() { if (!this.head) return ''; let result = ''; let temp = this.head; let count = 0; do { result += temp.data + ' -> '; temp = temp.next; count++; if (count > 10) return 'Infinite loop detected'; } while (temp !== this.head); result += 'back to ' + this.head.data; return result; } } const cll = new CircularLinkedList(); cll.insertAtBeginning(1); cll.insertAtBeginning(2); cll.insertAtBeginning(3); console.log(cll.printList());
Attempts:
2 left
💡 Hint
The last node must point to the new head to keep the list circular.
✗ Incorrect
Without updating the last node's next pointer, the list is broken and printList loops infinitely. The code detects this and returns 'Infinite loop detected'.
🧠 Conceptual
expertWhy is updating the last node's next pointer necessary when inserting at beginning?
In a circular linked list, when inserting a new node at the beginning, 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
The last node's next pointer must point to the new head to keep the circular link intact. Otherwise, traversal will not loop correctly.
