Challenge - 5 Problems
Circular List Master
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 output of the circular singly linked list after inserting nodes with values 10, 20, and 30 in that order?
DSA C++
#include <iostream> struct Node { int data; Node* next; Node(int val) : data(val), next(nullptr) {} }; class CircularSinglyLinkedList { public: Node* tail; CircularSinglyLinkedList() : tail(nullptr) {} void insert(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 print() { 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() { CircularSinglyLinkedList list; list.insert(10); list.insert(20); list.insert(30); list.print(); return 0; }
Attempts:
2 left
💡 Hint
Remember that insertion adds the new node after the tail and updates the tail to the new node.
✗ Incorrect
The insert function adds new nodes after the tail and moves the tail pointer to the new node. The print starts from tail->next, which is the head. So the order is 10 -> 20 -> 30 -> null.
❓ Predict Output
intermediateOutput after Deleting Head Node in Circular Singly Linked List
Given the circular singly linked list with nodes 5 -> 15 -> 25 -> null, what is the output after deleting the head node (node with value 5)?
DSA C++
#include <iostream> struct Node { int data; Node* next; Node(int val) : data(val), next(nullptr) {} }; class CircularSinglyLinkedList { public: Node* tail; CircularSinglyLinkedList() : tail(nullptr) {} void insert(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 deleteHead() { if (!tail) return; Node* head = tail->next; if (head == tail) { delete head; tail = nullptr; } else { tail->next = head->next; delete head; } } void print() { 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() { CircularSinglyLinkedList list; list.insert(5); list.insert(15); list.insert(25); list.deleteHead(); list.print(); return 0; }
Attempts:
2 left
💡 Hint
Deleting head means removing the node tail->next points to and updating tail->next.
✗ Incorrect
After deleting the head node (5), the new head is 15, and the list becomes 15 -> 25 -> null.
🔧 Debug
advancedIdentify the Bug in Circular Singly Linked List Insertion
What is the bug in the following insert function for a circular singly linked list?
DSA C++
void insert(int val) { Node* newNode = new Node(val); if (!tail) { tail = newNode; newNode->next = nullptr; } else { newNode->next = tail->next; tail->next = newNode; tail = newNode; } }
Attempts:
2 left
💡 Hint
In a circular list with one node, that node's next must point to itself.
✗ Incorrect
When the list is empty, the new node's next must point to itself to maintain circularity. Setting it to nullptr breaks the circular link.
❓ Predict Output
advancedOutput after Traversing Circular Singly Linked List with One Node
What is the output of the print function when the circular singly linked list contains only one node with value 42?
DSA C++
#include <iostream> struct Node { int data; Node* next; Node(int val) : data(val), next(nullptr) {} }; class CircularSinglyLinkedList { public: Node* tail; CircularSinglyLinkedList() : tail(nullptr) {} void insert(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 print() { 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() { CircularSinglyLinkedList list; list.insert(42); list.print(); return 0; }
Attempts:
2 left
💡 Hint
The print function prints nodes until it reaches the start node again, printing each node once.
✗ Incorrect
With one node, the print function prints its data once followed by '-> null'.
🧠 Conceptual
expertWhy Use Circular Singly Linked List Instead of Singly Linked List?
Which of the following is the main advantage of using a circular singly linked list over a regular singly linked list?
Attempts:
2 left
💡 Hint
Think about what happens when you reach the end of the list in both types.
✗ Incorrect
In a circular singly linked list, the last node points back to the first node, allowing continuous traversal without encountering null, unlike a regular singly linked list.
