Bird
Raised Fist0
DSA Cprogramming~10 mins

Insert at Beginning Head Insert in DSA C - Execution Trace

Choose your learning style10 modes available
Concept Flow - Insert at Beginning Head Insert
Create new node
↓
Set new_node->next = head
↓
Update head = new_node
↓
New node is now the first node in list
This flow shows how a new node is created and placed at the start of the linked list by adjusting pointers.
Execution Sample
DSA C
struct Node {
    int data;
    struct Node* next;
};

void insertAtBeginning(struct Node** head, int val) {
    struct Node* new_node = malloc(sizeof(struct Node));
    new_node->data = val;
    new_node->next = *head;
    *head = new_node;
}
This code creates a new node and inserts it at the beginning of the linked list.
Execution Table
StepOperationNodes in ListPointer ChangesVisual State
1Start with empty list∅head = ∅List is empty: head → ∅
2Create new node with data=10Node(10)new_node created, next unassignednew_node: [data:10 | next: ?]
3Set new_node->next = head (∅)Node(10)new_node->next → ∅new_node: [data:10 | next: ∅]
4Update head = new_nodeNode(10)head → new_nodehead → [data:10 | next: ∅]
5Insert new node with data=20 at beginningNode(20) → Node(10)new_node created, new_node->next = head, head updatedhead → [data:20 | next: ] → [data:10 | next: ∅]
6Insert new node with data=30 at beginningNode(30) → Node(20) → Node(10)new_node created, new_node->next = head, head updatedhead → [data:30 | next: ] → [data:20 | next: ] → [data:10 | next: ∅]
7End of insertionsNode(30) → Node(20) → Node(10)No pointer changesFinal list: head → 30 → 20 → 10 → ∅
💡 All insertions done, head points to newest node, list built from front
Variable Tracker
VariableStartAfter Step 2After Step 4After Step 5After Step 6Final
head∅∅[data:10 | next: ∅][data:20 | next: ][data:30 | next: ][data:30 | next: ]
new_node∅[data:10 | next: ?][data:10 | next: ∅][data:20 | next: ][data:30 | next: ]∅ (no longer needed)
Key Moments - 3 Insights
Why do we set new_node->next = head before updating head?
Because we want the new node to point to the current first node before head changes. See execution_table step 3 and 4 where new_node->next is set to old head, then head is updated to new_node.
What happens if the list is empty when inserting?
The new node's next points to null (∅), and head points to the new node. This is shown in steps 1 to 4 where head starts as ∅ and after insertion points to the new node.
How does the list grow after multiple insertions at beginning?
Each new node points to the previous head, making it the new first node. The list visually grows from the front as shown in steps 5 and 6.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 4, what does head point to?
AThe old head before insertion
BNull (∅)
CThe new node with data 10
DThe new node with data 20
💡 Hint
Check the Pointer Changes and Visual State columns at step 4 in execution_table
At which step does the list first contain two nodes?
AStep 5
BStep 4
CStep 3
DStep 6
💡 Hint
Look at the Nodes in List column to see when two nodes appear
If we skipped setting new_node->next = head, what would happen?
AThe list would be empty
BThe new node would not link to the rest of the list
CThe head pointer would not change
DThe new node would point to itself
💡 Hint
Refer to the key moment about why new_node->next is set before updating head
Concept Snapshot
Insert at Beginning (Head Insert):
- Create new node
- Set new_node->next = head
- Update head = new_node
- New node becomes first in list
- List grows by adding nodes at front
Full Transcript
This concept shows how to insert a new node at the beginning of a singly linked list. First, a new node is created with the given data. Then, the new node's next pointer is set to the current head of the list. Finally, the head pointer is updated to point to the new node. This makes the new node the first node in the list. Repeating this process adds nodes to the front, growing the list from the beginning.

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