Bird
Raised Fist0
DSA Cprogramming~20 mins

Push Using Linked List Node in DSA C - Practice Problems & Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Linked List Push Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2: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;
}
A20 -> 30 -> 10 -> NULL
B10 -> 20 -> 30 -> NULL
C30 -> 20 -> 10 -> NULL
DNULL
Attempts:
2 left
💡 Hint
Remember, push adds a new node at the beginning of the list.
❓ Predict Output
intermediate
2: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;
}
A5 -> 5 -> 10 -> NULL
B10 -> 5 -> 5 -> NULL
CNULL
D5 -> 10 -> 5 -> NULL
Attempts:
2 left
💡 Hint
Push adds nodes at the front, so last pushed is first.
❓ Predict Output
advanced
2: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;
}
ANULL
B3 -> 2 -> 1 -> NULL
CSegmentation fault
D1 -> 2 -> 3 -> NULL
Attempts:
2 left
💡 Hint
Check how the head pointer is passed and updated.
❓ Predict Output
advanced
2: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;
}
AUndefined behavior or crash
B100 -> NULL
CNULL
D200 -> 100 -> NULL
Attempts:
2 left
💡 Hint
Freeing head only frees one node, but list still points to freed memory.
🧠 Conceptual
expert
2: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*)?
ABecause passing struct Node* is faster and more efficient.
BBecause we need to modify the head pointer itself to point to the new node.
CBecause struct Node* cannot be used to access node data.
DBecause pointer to pointer automatically frees memory.
Attempts:
2 left
💡 Hint
Think about what happens when you want to change the start of the list.

Practice

(1/5)
1. What does the push operation do in a linked list when using nodes?
easy
A. Searches for a node with a specific value
B. Adds a new node at the beginning of the list
C. Removes the last node from the list
D. Deletes the entire linked list

Solution

  1. Step 1: Understand the push operation

    The push operation adds a new element to the linked list.
  2. Step 2: Identify where the new node is added

    In a linked list, push adds the new node at the start (head) of the list.
  3. Final Answer:

    Adds a new node at the beginning of the list -> Option B
  4. Quick Check:

    Push = Add at head [OK]
Hint: Push always adds at the start of the linked list [OK]
Common Mistakes:
  • Thinking push adds at the end
  • Confusing push with pop
  • Assuming push deletes nodes
2. Which of the following is the correct syntax to create a new node and push it onto the linked list in C?
easy
A. newNode = malloc(sizeof(Node)); newNode->prev = head; head = newNode;
B. newNode = malloc(Node); newNode->next = head; head = newNode;
C. newNode = malloc(sizeof(Node)); newNode->next = head; head = newNode;
D. newNode = malloc(sizeof(Node)); head = newNode->next; newNode = head;

Solution

  1. Step 1: Allocate memory correctly for new node

    Use malloc(sizeof(Node)) to allocate memory for the new node.
  2. Step 2: Link new node to current head and update head

    Set newNode->next to current head, then update head to newNode.
  3. Final Answer:

    newNode = malloc(sizeof(Node)); newNode->next = head; head = newNode; -> Option C
  4. Quick Check:

    Correct malloc and linking = newNode = malloc(sizeof(Node)); newNode->next = head; head = newNode; [OK]
Hint: Use sizeof(Node) with malloc and link newNode->next to head [OK]
Common Mistakes:
  • Forgetting sizeof in malloc
  • Assigning head before setting newNode->next
  • Using prev pointer in singly linked list
3. What will be the linked list after executing the following code?
typedef struct Node {
  int data;
  struct Node* next;
} Node;

Node* head = NULL;

void push(int val) {
  Node* newNode = malloc(sizeof(Node));
  newNode->data = val;
  newNode->next = head;
  head = newNode;
}

push(10);
push(20);
push(30);
medium
A. 30 -> 20 -> 10 -> null
B. 10 -> 20 -> 30 -> null
C. 20 -> 30 -> 10 -> null
D. 10 -> 30 -> 20 -> null

Solution

  1. Step 1: Trace each push call

    push(10) adds 10 at head: 10 -> null; push(20) adds 20 at head: 20 -> 10 -> null; push(30) adds 30 at head: 30 -> 20 -> 10 -> null.
  2. Step 2: Confirm final linked list order

    The last pushed value is at the head, so list is 30 -> 20 -> 10 -> null.
  3. Final Answer:

    30 -> 20 -> 10 -> null -> Option A
  4. Quick Check:

    Last pushed is head = 30 -> 20 -> 10 [OK]
Hint: Last pushed node is always at the head [OK]
Common Mistakes:
  • Assuming push adds at tail
  • Reading list in wrong order
  • Confusing push with append
4. Identify the error in this push function and select the correct fix:
void push(int val) {
  Node* newNode;
  newNode->data = val;
  newNode->next = head;
  head = newNode;
}
medium
A. head should be assigned before newNode->next
B. newNode->data should be assigned after head update
C. newNode->next should be NULL instead of head
D. newNode is not allocated memory; fix by malloc(sizeof(Node))

Solution

  1. Step 1: Check newNode initialization

    newNode is declared but not allocated memory with malloc, so it points to garbage.
  2. Step 2: Fix by allocating memory

    Use newNode = malloc(sizeof(Node)) before accessing newNode->data or newNode->next.
  3. Final Answer:

    newNode is not allocated memory; fix by malloc(sizeof(Node)) -> Option D
  4. Quick Check:

    Always malloc before using newNode [OK]
Hint: Always allocate memory before using new node [OK]
Common Mistakes:
  • Using uninitialized pointer
  • Assigning data before allocation
  • Changing head before linking newNode
5. You want to push multiple values 5, 15, 25 onto an empty linked list using the push function below. After pushing, you want to print the list from head to tail. Which code snippet correctly performs this and prints the list?
typedef struct Node {
  int data;
  struct Node* next;
} Node;

Node* head = NULL;

void push(int val) {
  Node* newNode = malloc(sizeof(Node));
  newNode->data = val;
  newNode->next = head;
  head = newNode;
}

// Your code here to push and print
hard
A. push(5); push(15); push(25); Node* temp = head; while(temp) { printf("%d -> ", temp->data); temp = temp->next; } printf("null\n");
B. push(5); push(15); push(25); Node* temp = head; while(temp->next) { printf("%d -> ", temp->data); temp = temp->next; } printf("null\n");
C. push(25); push(15); push(5); Node* temp = head; while(temp) { printf("%d -> ", temp->data); temp = temp->next; } printf("null\n");
D. push(5); push(15); push(25); Node* temp = head; while(temp) { printf("%d ", temp->data); temp = temp->next; }

Solution

  1. Step 1: Push values in correct order

    Push 5, then 15, then 25. Each push adds at head, so list is 25 -> 15 -> 5 -> null.
  2. Step 2: Print all nodes correctly

    Use a loop while(temp) to print each node's data followed by '->', then print 'null' after loop.
  3. Final Answer:

    push(5); push(15); push(25); Node* temp = head; while(temp) { printf("%d -> ", temp->data); temp = temp->next; } printf("null\n"); -> Option A
  4. Quick Check:

    Push order + full print loop = push(5); push(15); push(25); Node* temp = head; while(temp) { printf("%d -> ", temp->data); temp = temp->next; } printf("null\n"); [OK]
Hint: Push in order, print with while(temp) loop ending with null [OK]
Common Mistakes:
  • Pushing in reverse order
  • Using while(temp->next) and missing last node
  • Not printing 'null' at end
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard