Challenge - 5 Problems
Singly Linked List Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
What is the output of this linked list traversal?
Consider a singly linked list created with nodes containing values 10, 20, and 30 in that order. What will be printed after traversing the list?
DSA C
typedef struct Node {
int data;
struct Node* next;
} Node;
#include <stdio.h>
#include <stdlib.h>
int main() {
Node* head = malloc(sizeof(Node));
head->data = 10;
head->next = malloc(sizeof(Node));
head->next->data = 20;
head->next->next = malloc(sizeof(Node));
head->next->next->data = 30;
head->next->next->next = NULL;
Node* current = head;
while (current != NULL) {
printf("%d -> ", current->data);
current = current->next;
}
printf("null\n");
return 0;
}Attempts:
2 left
💡 Hint
Think about how nodes are linked and the order of traversal from head to tail.
✗ Incorrect
The list is created with nodes 10, 20, and 30 linked in that order. Traversing from head prints each node's data followed by an arrow, ending with null.
🧠 Conceptual
intermediate1:30remaining
What happens if you forget to set the last node's next pointer to NULL?
In a singly linked list, if the last node's next pointer is not set to NULL, what is the most likely outcome when traversing the list?
Attempts:
2 left
💡 Hint
Think about what NULL means in a linked list.
✗ Incorrect
The NULL pointer marks the end of the list. Without it, traversal continues into invalid memory, causing errors.
🔧 Debug
advanced2:30remaining
Why does this linked list insertion code cause a segmentation fault?
Examine the code below that tries to insert a new node at the beginning of a singly linked list. Identify the cause of the segmentation fault.
DSA C
typedef struct Node {
int data;
struct Node* next;
} Node;
void insertAtHead(Node* head, int value) {
Node* newNode = malloc(sizeof(Node));
newNode->data = value;
newNode->next = head;
head = newNode;
}
int main() {
Node* head = NULL;
insertAtHead(head, 5);
printf("%d\n", head->data);
return 0;
}Attempts:
2 left
💡 Hint
Consider how pointers are passed to functions in C.
✗ Incorrect
The function receives a copy of head pointer. Modifying it does not change the original head in main, so head remains NULL causing a fault when dereferenced.
📝 Syntax
advanced1:30remaining
Which option correctly defines a node struct for a singly linked list in C?
Choose the correct C struct definition for a singly linked list node holding an integer data.
Attempts:
2 left
💡 Hint
Remember how to refer to the struct type inside itself.
✗ Incorrect
Option B correctly uses 'struct Node* next;' inside the struct and typedefs it as Node.
🚀 Application
expert3:00remaining
What is the output after inserting nodes 1, 2, 3 at the head in order?
Starting with an empty singly linked list, you insert nodes with values 1, then 2, then 3 at the head. What is the final printed list?
DSA C
#include <stdio.h> #include <stdlib.h> typedef struct Node { int data; struct Node* next; } Node; void insertAtHead(Node** head, int value) { Node* newNode = malloc(sizeof(Node)); newNode->data = value; newNode->next = *head; *head = newNode; } void printList(Node* head) { Node* current = head; while (current != NULL) { printf("%d -> ", current->data); current = current->next; } printf("null\n"); } int main() { Node* head = NULL; insertAtHead(&head, 1); insertAtHead(&head, 2); insertAtHead(&head, 3); printList(head); return 0; }
Attempts:
2 left
💡 Hint
Inserting at head means new nodes become the first node.
✗ Incorrect
Each new node is added at the front, so the last inserted node (3) is at the head, followed by 2, then 1.
