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 Javascript
class Node { constructor(data) { this.data = data; this.next = null; } } class CircularLinkedList { constructor() { this.tail = null; } insertAtEnd(data) { const newNode = new Node(data); if (!this.tail) { newNode.next = newNode; this.tail = newNode; } else { newNode.next = this.tail.next; this.tail.next = newNode; this.tail = newNode; } } printList() { if (!this.tail) return "null"; let result = ""; let current = this.tail.next; do { result += current.data + " -> "; current = current.next; } while (current !== this.tail.next); result += "null"; 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 printList method starts from the node after tail (head) and prints all nodes until it loops back to head.
❓ Predict Output
intermediateOutput after inserting into an empty circular linked list
What is the printed state of the circular linked list after inserting the value 10 into an empty list?
DSA Javascript
class Node { constructor(data) { this.data = data; this.next = null; } } class CircularLinkedList { constructor() { this.tail = null; } insertAtEnd(data) { const newNode = new Node(data); if (!this.tail) { newNode.next = newNode; this.tail = newNode; } else { newNode.next = this.tail.next; this.tail.next = newNode; this.tail = newNode; } } printList() { if (!this.tail) return "null"; let result = ""; let current = this.tail.next; do { result += current.data + " -> "; current = current.next; } while (current !== this.tail.next); result += "null"; return result; } } const cll = new CircularLinkedList(); cll.insertAtEnd(10); console.log(cll.printList());
Attempts:
2 left
💡 Hint
Inserting into an empty circular linked list creates a single node pointing to itself.
✗ Incorrect
When the list is empty, the new node points to itself and becomes the tail. Printing starts from tail.next which is the node itself.
❓ Predict Output
advancedOutput after multiple insertions and one deletion in circular linked list
Given the code below, what is the printed state of the circular linked list after inserting 5, 10, 15 and then deleting the node with value 10?
DSA Javascript
class Node { constructor(data) { this.data = data; this.next = null; } } class CircularLinkedList { constructor() { this.tail = null; } insertAtEnd(data) { const newNode = new Node(data); if (!this.tail) { newNode.next = newNode; this.tail = newNode; } else { newNode.next = this.tail.next; this.tail.next = newNode; this.tail = newNode; } } deleteNode(data) { if (!this.tail) return; let current = this.tail.next; let prev = this.tail; do { if (current.data === data) { if (current === prev) { this.tail = null; } else { prev.next = current.next; if (current === this.tail) { this.tail = prev; } } return; } prev = current; current = current.next; } while (current !== this.tail.next); } printList() { if (!this.tail) return "null"; let result = ""; let current = this.tail.next; do { result += current.data + " -> "; current = current.next; } while (current !== this.tail.next); result += "null"; return result; } } const cll = new CircularLinkedList(); cll.insertAtEnd(5); cll.insertAtEnd(10); cll.insertAtEnd(15); cll.deleteNode(10); console.log(cll.printList());
Attempts:
2 left
💡 Hint
Deleting a node updates the links and possibly the tail pointer.
✗ Incorrect
Node with value 10 is removed. The list now has nodes 5 and 15 linked circularly. Printing starts from head (tail.next) which is 5.
🔧 Debug
advancedIdentify the error in insertAtEnd method of circular linked list
What error will occur when running the following insertAtEnd method code snippet?
DSA Javascript
insertAtEnd(data) {
const newNode = new Node(data);
if (this.tail === null) {
newNode.next = null;
this.tail = newNode;
} else {
newNode.next = this.tail.next;
this.tail.next = newNode;
this.tail = newNode;
}
}Attempts:
2 left
💡 Hint
In a circular linked list, tail.next should never be null.
✗ Incorrect
Setting newNode.next to null breaks the circular link. The print method loops infinitely because tail.next is null and the loop condition never ends.
❓ Predict Output
expertOutput after inserting nodes and traversing twice in circular linked list
What is the output of the code below that inserts nodes 7, 14, 21 and then prints the list twice in a row?
DSA Javascript
class Node { constructor(data) { this.data = data; this.next = null; } } class CircularLinkedList { constructor() { this.tail = null; } insertAtEnd(data) { const newNode = new Node(data); if (!this.tail) { newNode.next = newNode; this.tail = newNode; } else { newNode.next = this.tail.next; this.tail.next = newNode; this.tail = newNode; } } printList() { if (!this.tail) return "null"; let result = ""; let current = this.tail.next; do { result += current.data + " -> "; current = current.next; } while (current !== this.tail.next); result += "null"; return result; } } const cll = new CircularLinkedList(); cll.insertAtEnd(7); cll.insertAtEnd(14); cll.insertAtEnd(21); console.log(cll.printList()); console.log(cll.printList());
Attempts:
2 left
💡 Hint
Each printList call prints the full circular list once.
✗ Incorrect
printList starts from head and prints all nodes once until it loops back. Calling it twice prints the same list twice separately.
