Challenge - 5 Problems
Linked List Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of Traversing a Linked List
What is the printed output of the following C++ code that traverses and prints a singly linked list?
DSA C++
struct Node {
int data;
Node* next;
};
int main() {
Node n3 = {30, nullptr};
Node n2 = {20, &n3};
Node n1 = {10, &n2};
Node* head = &n1;
while (head != nullptr) {
std::cout << head->data << " -> ";
head = head->next;
}
std::cout << "null" << std::endl;
return 0;
}Attempts:
2 left
💡 Hint
Remember the traversal starts from the head and moves to next nodes until nullptr.
✗ Incorrect
The code starts at node n1 with data 10, then moves to n2 (20), then n3 (30), printing each data followed by ' -> '. Finally, it prints 'null'.
❓ Predict Output
intermediateOutput After Modifying Linked List Pointer
What is the output of this C++ code that modifies the head pointer before traversal?
DSA C++
struct Node {
int data;
Node* next;
};
int main() {
Node n3 = {30, nullptr};
Node n2 = {20, &n3};
Node n1 = {10, &n2};
Node* head = &n2; // start from second node
while (head != nullptr) {
std::cout << head->data << " -> ";
head = head->next;
}
std::cout << "null" << std::endl;
return 0;
}Attempts:
2 left
💡 Hint
The traversal starts from the node with data 20, not 10.
✗ Incorrect
The head pointer is set to n2, so traversal prints 20, then 30, then null.
🔧 Debug
advancedIdentify the Runtime Error in Linked List Traversal
What runtime error will occur when running this C++ code that attempts to traverse a linked list?
DSA C++
struct Node {
int data;
Node* next;
};
int main() {
Node* head = nullptr;
while (head->next != nullptr) {
std::cout << head->data << " -> ";
head = head->next;
}
std::cout << "null" << std::endl;
return 0;
}Attempts:
2 left
💡 Hint
Check what happens when head is nullptr and you try to access head->next.
✗ Incorrect
The code tries to access head->next when head is nullptr, causing a segmentation fault (runtime crash).
❓ Predict Output
advancedOutput of Recursive Linked List Print
What is the output of this recursive function that prints a linked list?
DSA C++
struct Node {
int data;
Node* next;
};
void printList(Node* node) {
if (node == nullptr) {
std::cout << "null" << std::endl;
return;
}
std::cout << node->data << " -> ";
printList(node->next);
}
int main() {
Node n3 = {30, nullptr};
Node n2 = {20, &n3};
Node n1 = {10, &n2};
printList(&n1);
return 0;
}Attempts:
2 left
💡 Hint
The function prints current node data then calls itself on next node.
✗ Incorrect
The recursion prints nodes in order: 10, 20, 30, then prints 'null' at the end.
🧠 Conceptual
expertNumber of Nodes in a Linked List After Traversal
Given a singly linked list with nodes containing values 5, 15, 25, 35 linked in order, what is the number of nodes counted after traversing the list from head to end?
Attempts:
2 left
💡 Hint
Count each node visited until you reach nullptr.
✗ Incorrect
The linked list has nodes with values 5, 15, 25, 35 linked in order, which is 4 nodes.
