Bird
Raised Fist0
DSA Cprogramming~20 mins

Insert at Beginning Head Insert in DSA C - Practice Problems & Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Head Insert 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 inserting nodes at the beginning?
Consider a singly linked list initially empty. We insert nodes with values 10, 20, and 30 at the beginning one by one. What is the final state of the linked list?
DSA C
struct Node {
    int data;
    struct Node* next;
};

struct Node* head = NULL;

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

int main() {
    insertAtBeginning(10);
    insertAtBeginning(20);
    insertAtBeginning(30);
    struct Node* temp = head;
    while(temp != NULL) {
        printf("%d -> ", temp->data);
        temp = temp->next;
    }
    printf("NULL\n");
    return 0;
}
A20 -> 30 -> 10 -> NULL
B10 -> 20 -> 30 -> NULL
CNULL
D30 -> 20 -> 10 -> NULL
Attempts:
2 left
💡 Hint
Remember, inserting at the beginning means the newest node becomes the head.
❓ Predict Output
intermediate
2:00remaining
What happens if we insert a node with value 0 at the beginning of a list with 5 -> 15 -> NULL?
Given a linked list with nodes 5 -> 15 -> NULL, we insert a node with value 0 at the beginning. What is the new list?
DSA C
struct Node {
    int data;
    struct Node* next;
};

struct Node* head = NULL;

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

int main() {
    struct Node* first = (struct Node*)malloc(sizeof(struct Node));
    struct Node* second = (struct Node*)malloc(sizeof(struct Node));
    first->data = 5;
    first->next = second;
    second->data = 15;
    second->next = NULL;
    head = first;

    insertAtBeginning(0);

    struct Node* temp = head;
    while(temp != NULL) {
        printf("%d -> ", temp->data);
        temp = temp->next;
    }
    printf("NULL\n");
    return 0;
}
A0 -> 5 -> 15 -> NULL
B5 -> 15 -> 0 -> NULL
C15 -> 5 -> 0 -> NULL
D5 -> 0 -> 15 -> NULL
Attempts:
2 left
💡 Hint
Inserting at the beginning changes the head to the new node.
🔧 Debug
advanced
2:00remaining
Why does this head insert code cause a segmentation fault?
Identify the bug in the following code that causes a segmentation fault when inserting at the beginning of a linked list.
DSA C
struct Node {
    int data;
    struct Node* next;
};

struct Node* head = NULL;

void insertAtBeginning(int val) {
    struct Node* newNode;
    newNode->data = val;
    newNode->next = head;
    head = newNode;
}

int main() {
    insertAtBeginning(10);
    return 0;
}
Ahead is not initialized to NULL
Bnext pointer is not set to NULL
CnewNode is not allocated memory before use, causing undefined behavior
Ddata field is assigned after setting next pointer
Attempts:
2 left
💡 Hint
Check if newNode points to valid memory before accessing its fields.
❓ Predict Output
advanced
2:00remaining
What is the output after multiple head insertions with this code?
Given the code below, what is printed after inserting 1, 2, 3 at the beginning in that order?
DSA C
struct Node {
    int data;
    struct Node* next;
};

struct Node* head = NULL;

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

int main() {
    insertAtBeginning(1);
    insertAtBeginning(2);
    insertAtBeginning(3);

    struct Node* temp = head;
    while(temp != NULL) {
        printf("%d ", temp->data);
        temp = temp->next;
    }
    return 0;
}
A1 2 3
B3 2 1
C1 3 2
D2 3 1
Attempts:
2 left
💡 Hint
The last inserted node becomes the head.
🧠 Conceptual
expert
1:00remaining
What is the time complexity of inserting a node at the beginning of a singly linked list?
Choose the correct time complexity for inserting a new node at the beginning (head) of a singly linked list.
AO(1) - Constant time
BO(n) - Linear time
CO(log n) - Logarithmic time
DO(n^2) - Quadratic time
Attempts:
1 left
💡 Hint
Inserting at the head does not require traversal of the list.

Practice

(1/5)
1. What does the "Insert at Beginning" operation do in a singly linked list?
easy
A. Deletes the first node of the list
B. Adds a new node at the end of the list
C. Adds a new node at the start of the list
D. Deletes the last node of the list

Solution

  1. Step 1: Understand the operation

    Inserting at the beginning means placing a new node before the current first node.
  2. Step 2: Effect on the list

    The new node becomes the head, pushing the old head to second position.
  3. Final Answer:

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

    Insert at beginning = Add new head [OK]
Hint: New node becomes head, old head moves next [OK]
Common Mistakes:
  • Confusing insert at beginning with insert at end
  • Thinking it deletes nodes
  • Assuming it inserts in the middle
2. Which of the following is the correct syntax to insert a new node at the beginning in C for a singly linked list?
easy
A. newNode->next = head; head = newNode;
B. head = newNode->next; newNode = head;
C. head->next = newNode; newNode = head;
D. newNode = head; head->next = newNode;

Solution

  1. Step 1: Assign newNode's next to current head

    This links the new node to the existing list.
  2. Step 2: Update head to newNode

    Now the new node is the first node in the list.
  3. Final Answer:

    newNode->next = head; head = newNode; -> Option A
  4. Quick Check:

    Link newNode to head, then update head [OK]
Hint: Point newNode->next to head, then head to newNode [OK]
Common Mistakes:
  • Swapping assignments order
  • Assigning head to newNode->next incorrectly
  • Not updating head pointer
3. What will be the linked list after inserting nodes with values 10, then 20, then 30 at the beginning in that order? Assume the list was initially empty.
medium
A. 10 -> 20 -> 30 -> null
B. 10 -> 30 -> 20 -> null
C. 20 -> 30 -> 10 -> null
D. 30 -> 20 -> 10 -> null

Solution

  1. Step 1: Insert 10 at beginning

    List becomes 10 -> null.
  2. Step 2: Insert 20 at beginning

    List becomes 20 -> 10 -> null.
  3. Step 3: Insert 30 at beginning

    List becomes 30 -> 20 -> 10 -> null.
  4. Final Answer:

    30 -> 20 -> 10 -> null -> Option D
  5. Quick Check:

    Last inserted is new head [OK]
Hint: Last inserted node is always head [OK]
Common Mistakes:
  • Assuming insertion at end
  • Reversing insertion order
  • Confusing node values order
4. Given the following code snippet, what is the error that prevents correct insertion at the beginning?
struct Node {
  int data;
  struct Node* next;
};

void insertAtBeginning(struct Node** head, int val) {
  struct Node* newNode = malloc(sizeof(struct Node));
  newNode->data = val;
  newNode->next = NULL;
  *head = newNode;
}
medium
A. malloc size is incorrect
B. newNode->next should point to current head before updating head
C. newNode->data is not assigned
D. head pointer should not be double pointer

Solution

  1. Step 1: Check newNode->next assignment

    It is set to NULL, losing the existing list.
  2. Step 2: Correct linking

    newNode->next should point to current head before head is updated.
  3. Final Answer:

    newNode->next should point to current head before updating head -> Option B
  4. Quick Check:

    Link newNode->next to old head before head update [OK]
Hint: Link newNode->next to old head before head update [OK]
Common Mistakes:
  • Setting newNode->next to NULL always
  • Not using double pointer for head
  • Forgetting to assign data
5. You have a singly linked list with nodes: 5 -> 15 -> 25 -> null. You want to insert a new node with value 10 at the beginning. After insertion, what will be the correct sequence of nodes?
hard
A. 10 -> 5 -> 15 -> 25 -> null
B. 5 -> 10 -> 15 -> 25 -> null
C. 10 -> 15 -> 25 -> 5 -> null
D. 5 -> 15 -> 25 -> 10 -> null

Solution

  1. Step 1: Current list is 5 -> 15 -> 25 -> null

    This is the starting list before insertion.
  2. Step 2: Insert 10 at beginning

    New node 10 points to current head (5), then head updated to 10.
  3. Final Answer:

    10 -> 5 -> 15 -> 25 -> null -> Option A
  4. Quick Check:

    New node becomes head, old head follows [OK]
Hint: New node points to old head, becomes new head [OK]
Common Mistakes:
  • Inserting at wrong position
  • Not updating head pointer
  • Linking nodes incorrectly
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