What if you could add new things to the start of a list instantly, without moving anything else?
Why Insert at Beginning Head Insert in DSA C?
Imagine you have a list of your favorite songs written on paper. Every time you find a new favorite, you want to add it to the very start of the list so you see it first.
Doing this by rewriting the entire list every time is tiring and slow.
Manually moving all songs down one by one to add a new favorite at the start takes a lot of time and effort.
It's easy to make mistakes like losing a song or mixing the order.
Using a linked list and inserting at the beginning means you just create a new song note and point it to the old first song.
No need to move anything else, making it fast and safe.
for (int i = length; i > 0; i--) { list[i] = list[i-1]; } list[0] = new_song;
new_node->next = head; head = new_node;
This lets you quickly add new items to the front of a list without disturbing the rest.
Adding the latest news headline at the top of a news feed so readers see it first.
Manual shifting is slow and error-prone.
Head insert in linked list is fast and simple.
Perfect for adding new items at the front instantly.
Practice
Solution
Step 1: Understand the operation
Inserting at the beginning means placing a new node before the current first node.Step 2: Effect on the list
The new node becomes the head, pushing the old head to second position.Final Answer:
Adds a new node at the start of the list -> Option CQuick Check:
Insert at beginning = Add new head [OK]
- Confusing insert at beginning with insert at end
- Thinking it deletes nodes
- Assuming it inserts in the middle
Solution
Step 1: Assign newNode's next to current head
This links the new node to the existing list.Step 2: Update head to newNode
Now the new node is the first node in the list.Final Answer:
newNode->next = head; head = newNode; -> Option AQuick Check:
Link newNode to head, then update head [OK]
- Swapping assignments order
- Assigning head to newNode->next incorrectly
- Not updating head pointer
Solution
Step 1: Insert 10 at beginning
List becomes 10 -> null.Step 2: Insert 20 at beginning
List becomes 20 -> 10 -> null.Step 3: Insert 30 at beginning
List becomes 30 -> 20 -> 10 -> null.Final Answer:
30 -> 20 -> 10 -> null -> Option DQuick Check:
Last inserted is new head [OK]
- Assuming insertion at end
- Reversing insertion order
- Confusing node values order
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;
}Solution
Step 1: Check newNode->next assignment
It is set to NULL, losing the existing list.Step 2: Correct linking
newNode->next should point to current head before head is updated.Final Answer:
newNode->next should point to current head before updating head -> Option BQuick Check:
Link newNode->next to old head before head update [OK]
- Setting newNode->next to NULL always
- Not using double pointer for head
- Forgetting to assign data
Solution
Step 1: Current list is 5 -> 15 -> 25 -> null
This is the starting list before insertion.Step 2: Insert 10 at beginning
New node 10 points to current head (5), then head updated to 10.Final Answer:
10 -> 5 -> 15 -> 25 -> null -> Option AQuick Check:
New node becomes head, old head follows [OK]
- Inserting at wrong position
- Not updating head pointer
- Linking nodes incorrectly
Ready to prove it?
Pass the test and this pattern moves unknown
