Challenge - 5 Problems
Linked List Length Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of Linked List Length Calculation
What is the output of this C code that calculates the length of a linked list?
DSA C
#include <stdio.h> #include <stdlib.h> struct Node { int data; struct Node* next; }; int getLength(struct Node* head) { int count = 0; struct Node* current = head; while (current != NULL) { count++; current = current->next; } return count; } int main() { struct Node* head = malloc(sizeof(struct Node)); head->data = 10; head->next = malloc(sizeof(struct Node)); head->next->data = 20; head->next->next = malloc(sizeof(struct Node)); head->next->next->data = 30; head->next->next->next = NULL; printf("Length: %d\n", getLength(head)); return 0; }
Attempts:
2 left
💡 Hint
Count each node until you reach NULL.
✗ Incorrect
The linked list has three nodes with data 10, 20, and 30. The function counts each node until it reaches NULL, so the length is 3.
❓ Predict Output
intermediate2:00remaining
Length of Empty Linked List
What is the output of this C code when the linked list is empty (head is NULL)?
DSA C
#include <stdio.h> struct Node { int data; struct Node* next; }; int getLength(struct Node* head) { int count = 0; struct Node* current = head; while (current != NULL) { count++; current = current->next; } return count; } int main() { struct Node* head = NULL; printf("Length: %d\n", getLength(head)); return 0; }
Attempts:
2 left
💡 Hint
An empty list has no nodes.
✗ Incorrect
Since head is NULL, the while loop does not run and count remains 0.
🔧 Debug
advanced2:00remaining
Identify the Bug in Length Calculation
What error will this code produce when trying to get the length of the linked list?
DSA C
#include <stdio.h> #include <stdlib.h> struct Node { int data; struct Node* next; }; int getLength(struct Node* head) { int count = 0; while (head->next != NULL) { count++; head = head->next; } return count; } int main() { struct Node* head = malloc(sizeof(struct Node)); head->data = 5; head->next = NULL; printf("Length: %d\n", getLength(head)); return 0; }
Attempts:
2 left
💡 Hint
Check the loop condition carefully.
✗ Incorrect
The loop counts nodes only while head->next is not NULL, so it stops before counting the last node. For a single node, count remains 0.
❓ Predict Output
advanced2:00remaining
Output After Adding a Node at the End
What is the output of this code after adding a new node at the end and calculating the length?
DSA C
#include <stdio.h> #include <stdlib.h> struct Node { int data; struct Node* next; }; int getLength(struct Node* head) { int count = 0; struct Node* current = head; while (current != NULL) { count++; current = current->next; } return count; } void addNodeEnd(struct Node** head_ref, int new_data) { struct Node* new_node = malloc(sizeof(struct Node)); new_node->data = new_data; new_node->next = NULL; if (*head_ref == NULL) { *head_ref = new_node; return; } struct Node* last = *head_ref; while (last->next != NULL) { last = last->next; } last->next = new_node; } int main() { struct Node* head = NULL; addNodeEnd(&head, 1); addNodeEnd(&head, 2); addNodeEnd(&head, 3); addNodeEnd(&head, 4); printf("Length: %d\n", getLength(head)); return 0; }
Attempts:
2 left
💡 Hint
Count all nodes after adding four nodes.
✗ Incorrect
Four nodes are added, so the length is 4.
🧠 Conceptual
expert2:00remaining
Time Complexity of Linked List Length Calculation
What is the time complexity of calculating the length of a singly linked list with n nodes using the standard iterative method?
Attempts:
2 left
💡 Hint
You must visit each node once to count.
✗ Incorrect
The function visits each node once, so the time grows linearly with the number of nodes.
