Bird
Raised Fist0
DSA C++programming~20 mins

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

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Head Insert Mastery
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 the following C++ code that inserts nodes at the beginning of a singly linked list. What will be the printed list after all insertions?
DSA C++
struct Node {
    int data;
    Node* next;
    Node(int val) : data(val), next(nullptr) {}
};

void insertAtBeginning(Node*& head, int val) {
    Node* newNode = new Node(val);
    newNode->next = head;
    head = newNode;
}

void printList(Node* head) {
    while (head != nullptr) {
        std::cout << head->data << " -> ";
        head = head->next;
    }
    std::cout << "null" << std::endl;
}

int main() {
    Node* head = nullptr;
    insertAtBeginning(head, 10);
    insertAtBeginning(head, 20);
    insertAtBeginning(head, 30);
    printList(head);
    return 0;
}
A10 -> 20 -> 30 -> null
B30 -> 20 -> 10 -> null
Cnull
D20 -> 30 -> 10 -> null
Attempts:
2 left
💡 Hint
Remember that inserting at the beginning means the new node becomes the new head.
🧠 Conceptual
intermediate
1:30remaining
What happens to the old head when a new node is inserted at the beginning?
When inserting a new node at the beginning of a singly linked list, what happens to the previous head node?
AIt becomes the second node, linked after the new head node.
BIt is deleted and replaced by the new node.
CIt remains the head node, and the new node is discarded.
DIt becomes the last node in the list.
Attempts:
2 left
💡 Hint
Think about how the new node's next pointer is set.
🔧 Debug
advanced
2:30remaining
Identify the error in this head insertion code
What error will this code cause when inserting at the beginning of a linked list?
DSA C++
struct Node {
    int data;
    Node* next;
};

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

int main() {
    Node* head = nullptr;
    insertAtBeginning(head, 5);
    insertAtBeginning(head, 10);
    // printList(head) assumed
    return 0;
}
ARuntime error due to null pointer dereference.
BCompilation error due to missing constructor.
CMemory leak because new nodes are not deleted.
DThe head pointer in main remains nullptr; list is not updated.
Attempts:
2 left
💡 Hint
Check how the head pointer is passed to the function.
❓ Predict Output
advanced
2:30remaining
Output after multiple head insertions and one deletion
Given the code below, what is the output after all operations?
DSA C++
struct Node {
    int data;
    Node* next;
    Node(int val) : data(val), next(nullptr) {}
};

void insertAtBeginning(Node*& head, int val) {
    Node* newNode = new Node(val);
    newNode->next = head;
    head = newNode;
}

void deleteHead(Node*& head) {
    if (head == nullptr) return;
    Node* temp = head;
    head = head->next;
    delete temp;
}

void printList(Node* head) {
    while (head != nullptr) {
        std::cout << head->data << " -> ";
        head = head->next;
    }
    std::cout << "null" << std::endl;
}

int main() {
    Node* head = nullptr;
    insertAtBeginning(head, 1);
    insertAtBeginning(head, 2);
    insertAtBeginning(head, 3);
    deleteHead(head);
    printList(head);
    return 0;
}
A2 -> 1 -> null
Bnull
C1 -> null
D3 -> 2 -> 1 -> null
Attempts:
2 left
💡 Hint
Deleting the head removes the first inserted node.
🧠 Conceptual
expert
2:00remaining
Why is passing head by reference important in head insertion?
In C++, why must the head pointer be passed by reference (Node*&) when inserting at the beginning of a linked list?
ATo prevent memory leaks caused by new node allocations.
BTo avoid copying the entire linked list when passing the head pointer.
CTo allow the function to modify the original head pointer and update the list correctly.
DTo ensure the function can delete nodes safely.
Attempts:
2 left
💡 Hint
Think about how pointers and references work in C++ function parameters.

Practice

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

Solution

  1. Step 1: Understand the operation

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

    The new node becomes the head, and its next pointer points to the old head.
  3. Final Answer:

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

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

Solution

  1. Step 1: Link new node to current head

    Set newNode->next to point to the current head node.
  2. Step 2: Update head to new node

    Assign head to newNode so it becomes the new first node.
  3. Final Answer:

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

    Link then update head [OK]
Hint: Set newNode->next first, then update head [OK]
Common Mistakes:
  • Updating head before linking newNode->next
  • Swapping pointers incorrectly
  • Assigning newNode to head->next
3. What will be the output linked list after executing this code?
struct Node { int data; Node* next; };
Node* head = nullptr;
Node* newNode = new Node{10, nullptr};
newNode->next = head;
head = newNode;
newNode = new Node{20, nullptr};
newNode->next = head;
head = newNode;

Print the list from head to end.
medium
A. 20 -> 10 -> null
B. 10 -> 20 -> null
C. null -> 10 -> 20
D. 10 -> null

Solution

  1. Step 1: Insert first node with data 10

    head points to node with 10, next is nullptr.
  2. Step 2: Insert second node with data 20 at beginning

    newNode->next = head (node 10), head = newNode (node 20).
  3. Final Answer:

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

    Last inserted node is head [OK]
Hint: Last inserted node is always head [OK]
Common Mistakes:
  • Assuming order is insertion order instead of reversed
  • Confusing next pointers
  • Ignoring nullptr termination
4. Identify the error in this code snippet for inserting at the beginning of a linked list:
Node* newNode = new Node();
newNode->data = 5;
head = newNode;
newNode->next = head;
medium
A. No error, code is correct
B. newNode->data is not initialized
C. head should be set to nullptr first
D. newNode->next should be assigned before head is updated

Solution

  1. Step 1: Check pointer assignments order

    Assigning head = newNode before setting newNode->next causes newNode->next to point to itself.
  2. Step 2: Correct order

    newNode->next should be set to current head before updating head to newNode.
  3. Final Answer:

    newNode->next should be assigned before head is updated -> Option D
  4. Quick Check:

    Link newNode->next before head update [OK]
Hint: Always link newNode->next before changing head [OK]
Common Mistakes:
  • Updating head before linking newNode->next
  • Forgetting to initialize newNode->data
  • Assuming no error in pointer assignments
5. Given a linked list with nodes 30 -> 20 -> 10 -> null, what will be the list after inserting a new node with data 40 at the beginning twice in a row?
hard
A. 40 -> 40 -> 30 -> 20 -> 10 -> null
B. 30 -> 20 -> 10 -> 40 -> 40 -> null
C. 40 -> 30 -> 20 -> 10 -> null
D. 40 -> 30 -> 20 -> 10 -> 40 -> null

Solution

  1. Step 1: Insert first 40 at beginning

    New head is 40, next points to old head 30.
  2. Step 2: Insert second 40 at beginning

    New head is another 40, next points to previous 40 node.
  3. Final Answer:

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

    Each insert adds new head [OK]
Hint: Each insert adds a new head node [OK]
Common Mistakes:
  • Assuming duplicate nodes merge
  • Inserting at end instead of beginning
  • Losing track of next pointers
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