Challenge - 5 Problems
Linked List Traversal Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output 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
#include <stdio.h> #include <stdlib.h> struct Node { int data; struct Node* next; }; void printList(struct Node* head) { struct Node* current = head; while (current != NULL) { printf("%d -> ", current->data); current = current->next; } printf("NULL\n"); } int main() { struct Node* head = malloc(sizeof(struct Node)); struct Node* second = malloc(sizeof(struct Node)); struct Node* third = malloc(sizeof(struct Node)); head->data = 10; head->next = second; second->data = 20; second->next = third; third->data = 30; third->next = NULL; printList(head); return 0; }
Attempts:
2 left
💡 Hint
Follow the next pointers from head until NULL is reached.
✗ Incorrect
The code creates three nodes linked in order: 10, 20, 30. The printList function prints each node's data followed by '->' until it reaches NULL, then prints 'NULL'.
❓ Predict Output
intermediate2:00remaining
Output After Modifying Linked List Pointer
What will be the output of the following code that modifies the linked list before printing?
DSA C
#include <stdio.h> #include <stdlib.h> struct Node { int data; struct Node* next; }; void printList(struct Node* head) { struct Node* current = head; while (current != NULL) { printf("%d -> ", current->data); current = current->next; } printf("NULL\n"); } int main() { struct Node* head = malloc(sizeof(struct Node)); struct Node* second = malloc(sizeof(struct Node)); struct Node* third = malloc(sizeof(struct Node)); head->data = 1; head->next = second; second->data = 2; second->next = third; third->data = 3; third->next = NULL; // Modify the list second->next = NULL; printList(head); return 0; }
Attempts:
2 left
💡 Hint
Check how the next pointer of the second node is changed.
✗ Incorrect
The second node's next pointer is set to NULL, so the list ends after 2. The third node is disconnected and not printed.
🔧 Debug
advanced2:00remaining
Identify the Error in Linked List Traversal
What error will occur when running this code that attempts to print a linked list?
DSA C
#include <stdio.h> #include <stdlib.h> struct Node { int data; struct Node* next; }; void printList(struct Node* head) { struct Node* current = head; while (current->next != NULL) { printf("%d -> ", current->data); current = current->next; } printf("%d -> NULL\n", current->data); } int main() { struct Node* head = malloc(sizeof(struct Node)); head->data = 5; head->next = NULL; printList(head); return 0; }
Attempts:
2 left
💡 Hint
Check the loop condition and how it handles single-node list.
✗ Incorrect
The loop runs while current->next != NULL. For a single node with next NULL, loop skips and prints the last node's data correctly.
❓ Predict Output
advanced2:00remaining
Output of Recursive Linked List Print
What is the output of this recursive function printing a linked list?
DSA C
#include <stdio.h> #include <stdlib.h> struct Node { int data; struct Node* next; }; void printListRecursive(struct Node* node) { if (node == NULL) { printf("NULL\n"); return; } printf("%d -> ", node->data); printListRecursive(node->next); } int main() { struct Node* head = malloc(sizeof(struct Node)); struct Node* second = malloc(sizeof(struct Node)); head->data = 7; head->next = second; second->data = 14; second->next = NULL; printListRecursive(head); return 0; }
Attempts:
2 left
💡 Hint
Think about how recursion prints nodes in order.
✗ Incorrect
The function prints current node data then calls itself on next node, so output is in list order ending with NULL.
🧠 Conceptual
expert2:00remaining
Number of Nodes After Partial Traversal
Given a linked list with 5 nodes, if you traverse the list with the following code snippet, how many nodes will be counted?
DSA C
int countNodes(struct Node* head) { int count = 0; struct Node* current = head; while (current != NULL && count < 3) { count++; current = current->next; } return count; }
Attempts:
2 left
💡 Hint
The loop stops when count reaches 3 or list ends.
✗ Incorrect
The loop counts nodes but stops after counting 3 nodes even if list has 5 nodes.
