Challenge - 5 Problems
Circular List Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput after inserting nodes at the beginning
What is the printed state of the circular linked list after inserting 10, then 20, then 30 at the beginning?
DSA C++
struct Node {
int data;
Node* next;
};
Node* insertAtBeginning(Node* head, int data) {
Node* newNode = new Node{data, nullptr};
if (!head) {
newNode->next = newNode;
return newNode;
}
Node* temp = head;
while (temp->next != head) {
temp = temp->next;
}
temp->next = newNode;
newNode->next = head;
return newNode;
}
void printList(Node* head) {
if (!head) return;
Node* temp = head;
do {
std::cout << temp->data << " -> ";
temp = temp->next;
} while (temp != head);
std::cout << "null" << std::endl;
}
int main() {
Node* head = nullptr;
head = insertAtBeginning(head, 10);
head = insertAtBeginning(head, 20);
head = insertAtBeginning(head, 30);
printList(head);
return 0;
}Attempts:
2 left
💡 Hint
Remember that inserting at the beginning means the new node becomes the new head.
✗ Incorrect
Each insertion at the beginning adds a new node before the current head and updates the head pointer. The last inserted node is the head, so the list prints starting from 30, then 20, then 10, then loops back.
❓ Predict Output
intermediateOutput after inserting a single node in an empty list
What is the printed state of the circular linked list after inserting only one node with value 5 at the beginning?
DSA C++
struct Node {
int data;
Node* next;
};
Node* insertAtBeginning(Node* head, int data) {
Node* newNode = new Node{data, nullptr};
if (!head) {
newNode->next = newNode;
return newNode;
}
Node* temp = head;
while (temp->next != head) {
temp = temp->next;
}
temp->next = newNode;
newNode->next = head;
return newNode;
}
void printList(Node* head) {
if (!head) return;
Node* temp = head;
do {
std::cout << temp->data << " -> ";
temp = temp->next;
} while (temp != head);
std::cout << "null" << std::endl;
}
int main() {
Node* head = nullptr;
head = insertAtBeginning(head, 5);
printList(head);
return 0;
}Attempts:
2 left
💡 Hint
A single node in a circular linked list points to itself.
✗ Incorrect
When the list is empty, the new node points to itself, so printing starts and ends at that node, showing just one value.
🔧 Debug
advancedIdentify the error in the insertion code
What error will occur when running this insertion code for a circular linked list?
DSA C++
Node* insertAtBeginning(Node* head, int data) { Node* newNode = new Node{data, nullptr}; if (!head) { newNode->next = nullptr; return newNode; } Node* temp = head; while (temp->next != head) { temp = temp->next; } temp->next = newNode; newNode->next = head; return newNode; }
Attempts:
2 left
💡 Hint
Check what happens when the list is empty and how the new node's next pointer is set.
✗ Incorrect
When the list is empty, newNode->next is set to nullptr instead of pointing to itself, breaking the circular property. Traversing the list will not loop back, causing infinite loops or crashes.
❓ Predict Output
advancedOutput after multiple insertions and one deletion
Given the code inserts 1, 2, 3 at beginning and then deletes the node with value 2, what is the printed list?
DSA C++
struct Node {
int data;
Node* next;
};
Node* insertAtBeginning(Node* head, int data) {
Node* newNode = new Node{data, nullptr};
if (!head) {
newNode->next = newNode;
return newNode;
}
Node* temp = head;
while (temp->next != head) {
temp = temp->next;
}
temp->next = newNode;
newNode->next = head;
return newNode;
}
Node* deleteNode(Node* head, int key) {
if (!head) return nullptr;
if (head->data == key && head->next == head) {
delete head;
return nullptr;
}
Node* curr = head;
Node* prev = nullptr;
while (curr->data != key) {
prev = curr;
curr = curr->next;
if (curr == head) return head;
}
if (curr == head) {
prev = head;
while (prev->next != head) prev = prev->next;
head = curr->next;
prev->next = head;
delete curr;
} else {
prev->next = curr->next;
delete curr;
}
return head;
}
void printList(Node* head) {
if (!head) return;
Node* temp = head;
do {
std::cout << temp->data << " -> ";
temp = temp->next;
} while (temp != head);
std::cout << "null" << std::endl;
}
int main() {
Node* head = nullptr;
head = insertAtBeginning(head, 1);
head = insertAtBeginning(head, 2);
head = insertAtBeginning(head, 3);
head = deleteNode(head, 2);
printList(head);
return 0;
}Attempts:
2 left
💡 Hint
After deletion, the node with value 2 is removed, and the list remains circular.
✗ Incorrect
Insertion order makes list: 3 -> 2 -> 1 -> null. Deleting 2 removes that node, so list is 3 -> 1 -> null.
🧠 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 node?
Attempts:
2 left
💡 Hint
Think about what keeps the list circular.
✗ Incorrect
The last node must point to the new head to keep the circular structure intact. Without this, the list breaks and is no longer circular.
