Challenge - 5 Problems
Linked List Push Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
What is the output after pushing nodes?
Consider a linked list initially empty. We push nodes with values 10, 20, then 30 using the push function below. What is the printed linked list?
DSA C
#include <stdio.h> #include <stdlib.h> struct Node { int data; struct Node* next; }; void push(struct Node** head_ref, int new_data) { struct Node* new_node = (struct Node*) malloc(sizeof(struct Node)); new_node->data = new_data; new_node->next = *head_ref; *head_ref = new_node; } void printList(struct Node* node) { while (node != NULL) { printf("%d -> ", node->data); node = node->next; } printf("NULL\n"); } int main() { struct Node* head = NULL; push(&head, 10); push(&head, 20); push(&head, 30); printList(head); return 0; }
Attempts:
2 left
💡 Hint
Remember, push adds a new node at the beginning of the list.
✗ Incorrect
Each push inserts the new node at the head, so the last pushed node appears first.
❓ Predict Output
intermediate2:00remaining
What is the output after pushing nodes with duplicate values?
Given the push function, what is the linked list after pushing 5, 10, 5 in that order?
DSA C
#include <stdio.h> #include <stdlib.h> struct Node { int data; struct Node* next; }; void push(struct Node** head_ref, int new_data) { struct Node* new_node = (struct Node*) malloc(sizeof(struct Node)); new_node->data = new_data; new_node->next = *head_ref; *head_ref = new_node; } void printList(struct Node* node) { while (node != NULL) { printf("%d -> ", node->data); node = node->next; } printf("NULL\n"); } int main() { struct Node* head = NULL; push(&head, 5); push(&head, 10); push(&head, 5); printList(head); return 0; }
Attempts:
2 left
💡 Hint
Push adds nodes at the front, so last pushed is first.
✗ Incorrect
Nodes are added at the head, so the last pushed 5 is first, then 10, then first 5.
❓ Predict Output
advanced2:00remaining
What is the output after pushing nodes and modifying head incorrectly?
What happens if we push nodes but forget to update the head pointer correctly? Consider this code snippet:
DSA C
#include <stdio.h> #include <stdlib.h> struct Node { int data; struct Node* next; }; void push(struct Node* head, int new_data) { struct Node* new_node = (struct Node*) malloc(sizeof(struct Node)); new_node->data = new_data; new_node->next = head; head = new_node; } void printList(struct Node* node) { while (node != NULL) { printf("%d -> ", node->data); node = node->next; } printf("NULL\n"); } int main() { struct Node* head = NULL; push(head, 1); push(head, 2); push(head, 3); printList(head); return 0; }
Attempts:
2 left
💡 Hint
Check how the head pointer is passed and updated.
✗ Incorrect
The push function receives head by value, so changes do not affect main's head. The list remains empty.
❓ Predict Output
advanced2:00remaining
What is the output after pushing nodes and freeing memory incorrectly?
What happens if we push nodes and then free the head pointer before printing? Consider this code:
DSA C
#include <stdio.h> #include <stdlib.h> struct Node { int data; struct Node* next; }; void push(struct Node** head_ref, int new_data) { struct Node* new_node = (struct Node*) malloc(sizeof(struct Node)); new_node->data = new_data; new_node->next = *head_ref; *head_ref = new_node; } void printList(struct Node* node) { while (node != NULL) { printf("%d -> ", node->data); node = node->next; } printf("NULL\n"); } int main() { struct Node* head = NULL; push(&head, 100); push(&head, 200); free(head); printList(head); return 0; }
Attempts:
2 left
💡 Hint
Freeing head only frees one node, but list still points to freed memory.
✗ Incorrect
Freeing head frees only the first node, but printList accesses freed memory causing undefined behavior.
🧠 Conceptual
expert2:00remaining
Why must push use a pointer to pointer for head?
In linked list push operations, why do we pass a pointer to the head pointer (struct Node**) instead of just the head pointer (struct Node*)?
Attempts:
2 left
💡 Hint
Think about what happens when you want to change the start of the list.
✗ Incorrect
Passing a pointer to pointer allows the function to update the head pointer in the caller, so the new node becomes the first node.
