Challenge - 5 Problems
Circular Linked List Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput after inserting nodes at the end of a circular linked list
What is the printed state of the circular linked list after inserting 10, 20, and 30 at the end?
DSA C++
struct Node {
int data;
Node* next;
Node(int val) : data(val), next(nullptr) {}
};
class CircularLinkedList {
public:
Node* tail;
CircularLinkedList() : tail(nullptr) {}
void insertEnd(int val) {
Node* newNode = new Node(val);
if (!tail) {
tail = newNode;
tail->next = tail;
} else {
newNode->next = tail->next;
tail->next = newNode;
tail = newNode;
}
}
void printList() {
if (!tail) return;
Node* temp = tail->next;
do {
std::cout << temp->data << " -> ";
temp = temp->next;
} while (temp != tail->next);
std::cout << "null" << std::endl;
}
};
int main() {
CircularLinkedList cll;
cll.insertEnd(10);
cll.insertEnd(20);
cll.insertEnd(30);
cll.printList();
return 0;
}Attempts:
2 left
💡 Hint
Remember that in a circular linked list, the tail's next points to the head, so printing starts from tail->next.
✗ Incorrect
Inserting at the end updates the tail to the new node. The list starts printing from tail->next, which is the head. After inserting 10, 20, and 30, the order is 10 -> 20 -> 30 -> null.
❓ Predict Output
intermediateOutput after inserting a single node in an empty circular linked list
What is the printed state of the circular linked list after inserting only one node with value 5 at the end?
DSA C++
struct Node {
int data;
Node* next;
Node(int val) : data(val), next(nullptr) {}
};
class CircularLinkedList {
public:
Node* tail;
CircularLinkedList() : tail(nullptr) {}
void insertEnd(int val) {
Node* newNode = new Node(val);
if (!tail) {
tail = newNode;
tail->next = tail;
} else {
newNode->next = tail->next;
tail->next = newNode;
tail = newNode;
}
}
void printList() {
if (!tail) return;
Node* temp = tail->next;
do {
std::cout << temp->data << " -> ";
temp = temp->next;
} while (temp != tail->next);
std::cout << "null" << std::endl;
}
};
int main() {
CircularLinkedList cll;
cll.insertEnd(5);
cll.printList();
return 0;
}Attempts:
2 left
💡 Hint
A single node in a circular linked list points to itself.
✗ Incorrect
When only one node is inserted, tail points to it and its next points to itself. Printing starts at tail->next (the node itself) and stops after one iteration, printing '5 -> null'.
🔧 Debug
advancedIdentify the bug causing infinite loop in circular linked list insertion
Given the following insertEnd function, what is the bug causing an infinite loop when printing the list?
DSA C++
void insertEnd(int val) { Node* newNode = new Node(val); if (!tail) { tail = newNode; tail->next = nullptr; } else { newNode->next = tail->next; tail->next = newNode; tail = newNode; } }
Attempts:
2 left
💡 Hint
In a circular linked list, the last node's next must point to the head node, not nullptr.
✗ Incorrect
Setting tail->next to nullptr breaks the circular link. The print function loops infinitely because it never reaches the start again.
🧠 Conceptual
advancedUnderstanding tail pointer role in circular linked list insertion
Why is the tail pointer updated to the new node after inserting at the end of a circular linked list?
Attempts:
2 left
💡 Hint
Think about where new nodes are added and how tail helps in insertion.
✗ Incorrect
Tail points to the last node. When a new node is inserted at the end, tail must update to it to keep track of the last node for future insertions.
❓ Predict Output
expertOutput after multiple insertions and one traversal in circular linked list
What is the output of the following code snippet?
DSA C++
struct Node {
int data;
Node* next;
Node(int val) : data(val), next(nullptr) {}
};
class CircularLinkedList {
public:
Node* tail;
CircularLinkedList() : tail(nullptr) {}
void insertEnd(int val) {
Node* newNode = new Node(val);
if (!tail) {
tail = newNode;
tail->next = tail;
} else {
newNode->next = tail->next;
tail->next = newNode;
tail = newNode;
}
}
int countNodes() {
if (!tail) return 0;
int count = 0;
Node* temp = tail->next;
do {
count++;
temp = temp->next;
} while (temp != tail->next);
return count;
}
};
int main() {
CircularLinkedList cll;
cll.insertEnd(1);
cll.insertEnd(2);
cll.insertEnd(3);
cll.insertEnd(4);
int total = cll.countNodes();
std::cout << total << std::endl;
return 0;
}Attempts:
2 left
💡 Hint
Count nodes by traversing from tail->next until you reach the start again.
✗ Incorrect
Four nodes are inserted. The countNodes function traverses the circular list once, counting all nodes, resulting in 4.
