Challenge - 5 Problems
Circular Linked List Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of inserting a node at the end of a circular linked list
What is the printed state of the circular linked list after inserting the value 4 at the end?
DSA Typescript
class Node { value: number; next: Node | null; constructor(value: number) { this.value = value; this.next = null; } } class CircularLinkedList { tail: Node | null = null; insertAtEnd(value: number) { const newNode = new Node(value); if (!this.tail) { newNode.next = newNode; this.tail = newNode; } else { newNode.next = this.tail.next; this.tail.next = newNode; this.tail = newNode; } } printList(): string { if (!this.tail) return "null"; let result = ""; let current = this.tail.next; do { result += current!.value + " -> "; current = current!.next; } while (current !== this.tail.next); result += "(back to head)"; return result; } } const cll = new CircularLinkedList(); cll.insertAtEnd(1); cll.insertAtEnd(2); cll.insertAtEnd(3); cll.insertAtEnd(4); console.log(cll.printList());
Attempts:
2 left
💡 Hint
Remember that in a circular linked list, the tail's next points to the head.
✗ Incorrect
The insertAtEnd method adds the new node after the tail and updates the tail to the new node. The list after inserting 4 is 1 -> 2 -> 3 -> 4, and then it loops back to 1.
🧠 Conceptual
intermediateUnderstanding tail pointer in circular linked list insertion
Why do we update the tail pointer to the new node when inserting at the end of a circular linked list?
Attempts:
2 left
💡 Hint
Think about what the tail pointer represents in a circular linked list.
✗ Incorrect
The tail pointer always points to the last node in the list. When inserting at the end, the new node is the last node, so tail must be updated to it.
🔧 Debug
advancedIdentify the error in this circular linked list insertion code
What error will this code produce when inserting a node at the end of a circular linked list?
DSA Typescript
class Node { value: number; next: Node | null; constructor(value: number) { this.value = value; this.next = null; } } class CircularLinkedList { tail: Node | null = null; insertAtEnd(value: number) { const newNode = new Node(value); if (!this.tail) { newNode.next = null; this.tail = newNode; } else { newNode.next = this.tail.next; this.tail.next = newNode; this.tail = newNode; } } } const cll = new CircularLinkedList(); cll.insertAtEnd(1); cll.insertAtEnd(2);
Attempts:
2 left
💡 Hint
Check how the next pointer is assigned when the list is empty.
✗ Incorrect
When the list is empty, newNode.next should point to itself to maintain circularity. Assigning null breaks the circular property, making the list linear.
❓ Predict Output
advancedOutput after multiple insertions in circular linked list
What is the printed state of the circular linked list after these insertions?
DSA Typescript
class Node { value: number; next: Node | null; constructor(value: number) { this.value = value; this.next = null; } } class CircularLinkedList { tail: Node | null = null; insertAtEnd(value: number) { const newNode = new Node(value); if (!this.tail) { newNode.next = newNode; this.tail = newNode; } else { newNode.next = this.tail.next; this.tail.next = newNode; this.tail = newNode; } } printList(): string { if (!this.tail) return "null"; let result = ""; let current = this.tail.next; do { result += current!.value + " -> "; current = current!.next; } while (current !== this.tail.next); result += "(back to head)"; return result; } } const cll = new CircularLinkedList(); cll.insertAtEnd(10); cll.insertAtEnd(20); cll.insertAtEnd(30); cll.insertAtEnd(40); cll.insertAtEnd(50); console.log(cll.printList());
Attempts:
2 left
💡 Hint
Remember the tail points to the last inserted node, and tail.next is the head.
✗ Incorrect
The list after inserting 10, 20, 30, 40, 50 at the end is 10 -> 20 -> 30 -> 40 -> 50, then loops back to 10.
🧠 Conceptual
expertWhy is a circular linked list useful compared to a singly linked list?
Which of the following is the main advantage of using a circular linked list over a singly linked list?
Attempts:
2 left
💡 Hint
Think about what happens when you reach the end of a singly linked list.
✗ Incorrect
A circular linked list connects the last node back to the first, allowing continuous traversal without encountering null, unlike singly linked lists.
