Challenge - 5 Problems
Linked List Pop Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
What is the output after popping the head node from the linked list?
Consider a singly linked list with nodes containing values 10 -> 20 -> 30 -> null. The pop operation removes the head node. What is the state of the list after one pop?
DSA C
typedef struct Node {
int data;
struct Node* next;
} Node;
#include <stdio.h>
#include <stdlib.h>
Node* pop(Node** head) {
if (*head == NULL) return NULL;
Node* temp = *head;
*head = (*head)->next;
temp->next = NULL;
return temp;
}
void printList(Node* head) {
while (head != NULL) {
printf("%d -> ", head->data);
head = head->next;
}
printf("null\n");
}
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* popped = pop(&head);
printList(head);
free(popped);
// Free remaining nodes omitted for brevity
return 0;
}Attempts:
2 left
💡 Hint
Pop removes the first node and moves the head pointer to the next node.
✗ Incorrect
The pop function removes the head node (value 10) and updates the head pointer to the next node (value 20). The list after popping is 20 -> 30 -> null.
❓ Predict Output
intermediate2:00remaining
What is the output after popping all nodes one by one?
Given a linked list 5 -> 15 -> 25 -> null, pop nodes until the list is empty. What is printed after each pop?
DSA C
typedef struct Node {
int data;
struct Node* next;
} Node;
#include <stdio.h>
#include <stdlib.h>
Node* pop(Node** head) {
if (*head == NULL) return NULL;
Node* temp = *head;
*head = (*head)->next;
temp->next = NULL;
return temp;
}
void printList(Node* head) {
while (head != NULL) {
printf("%d -> ", head->data);
head = head->next;
}
printf("null\n");
}
int main() {
Node* head = malloc(sizeof(Node));
head->data = 5;
head->next = malloc(sizeof(Node));
head->next->data = 15;
head->next->next = malloc(sizeof(Node));
head->next->next->data = 25;
head->next->next->next = NULL;
for (int i = 0; i < 3; i++) {
Node* popped = pop(&head);
printList(head);
free(popped);
}
return 0;
}Attempts:
2 left
💡 Hint
Each pop removes the head node and updates the list.
✗ Incorrect
After first pop, list is 15 -> 25 -> null; after second pop, 25 -> null; after third pop, list is empty (null).
❓ Predict Output
advanced2:00remaining
What error occurs when popping from an empty linked list?
What happens if you call pop on a linked list where the head pointer is NULL?
DSA C
typedef struct Node {
int data;
struct Node* next;
} Node;
#include <stdio.h>
#include <stdlib.h>
Node* pop(Node** head) {
if (*head == NULL) return NULL;
Node* temp = *head;
*head = (*head)->next;
temp->next = NULL;
return temp;
}
int main() {
Node* head = NULL;
Node* popped = pop(&head);
if (popped == NULL) {
printf("List is empty, nothing to pop.\n");
}
return 0;
}Attempts:
2 left
💡 Hint
Check if head is NULL before popping.
✗ Incorrect
The pop function checks if the head is NULL and returns NULL safely, so no crash occurs. The program prints the message indicating the list is empty.
❓ Predict Output
advanced2:00remaining
What is the output after popping the last node from the list?
Given a linked list with a single node 42 -> null, what is the list state after popping once?
DSA C
typedef struct Node {
int data;
struct Node* next;
} Node;
#include <stdio.h>
#include <stdlib.h>
Node* pop(Node** head) {
if (*head == NULL) return NULL;
Node* temp = *head;
*head = (*head)->next;
temp->next = NULL;
return temp;
}
void printList(Node* head) {
while (head != NULL) {
printf("%d -> ", head->data);
head = head->next;
}
printf("null\n");
}
int main() {
Node* head = malloc(sizeof(Node));
head->data = 42;
head->next = NULL;
Node* popped = pop(&head);
printList(head);
free(popped);
return 0;
}Attempts:
2 left
💡 Hint
Popping the only node empties the list.
✗ Incorrect
After popping the only node, head becomes NULL, so printing the list shows null.
🧠 Conceptual
expert2:00remaining
What is the time complexity of popping the head node from a singly linked list?
Consider a singly linked list with n nodes. What is the time complexity of the pop operation that removes the head node?
Attempts:
2 left
💡 Hint
Pop only changes the head pointer and frees one node.
✗ Incorrect
Popping the head node requires updating the head pointer and freeing one node, which takes constant time regardless of list size.
