What if you could remove the first item instantly without moving everything else?
Why Delete Node at Beginning in DSA C++?
Imagine you have a long paper list of names, and you want to remove the first name every time someone leaves. Doing this by hand means rewriting the whole list each time to shift names up.
Manually rewriting or shifting all names is slow and tiring. It's easy to make mistakes, like skipping a name or losing track of the order.
Using a linked list, you can simply change the starting point to the next name, instantly removing the first one without touching the rest.
for (int i = 1; i < n; i++) { arr[i-1] = arr[i]; } n--;
Node* temp = head; head = head->next; delete temp;
This lets you remove the first item instantly, making your program fast and error-free.
Think of a queue at a ticket counter where the first person leaves after buying a ticket; the next person becomes first without rearranging everyone.
Manual shifting is slow and error-prone.
Deleting the first node just moves the head pointer.
This makes removal quick and simple.
Practice
Solution
Step 1: Understand the head pointer role
The head pointer points to the first node in the list.Step 2: Delete the first node by moving head
To delete the first node, update head to point to the second node, removing the first node from the list.Final Answer:
The head pointer moves to the second node, and the first node is removed. -> Option AQuick Check:
Deleting first node means head moves forward [OK]
- Deleting the wrong node (not the first)
- Not updating the head pointer
- Assuming list becomes empty always
- Confusing first and second nodes
Solution
Step 1: Store the current head node
We save the current head in a temporary pointer to delete it safely later.Step 2: Move head to next node and delete old head
Update head to point to the next node, then delete the old head using the temporary pointer.Final Answer:
Node* temp = head; head = head->next; delete temp; -> Option DQuick Check:
Save old head, move head, delete old head [OK]
- Deleting head before moving pointer
- Not saving old head before deleting
- Assigning head incorrectly
- Deleting head twice
struct Node {
int data;
Node* next;
};
Node* head = new Node{10, new Node{20, new Node{30, nullptr}}};
Node* temp = head;
head = head->next;
delete temp;
Node* curr = head;
while(curr != nullptr) {
std::cout << curr->data << " -> ";
curr = curr->next;
}
std::cout << "null";Solution
Step 1: Initial linked list setup
The list starts as 10 -> 20 -> 30 -> null.Step 2: Delete first node and update head
We save head (10) in temp, move head to 20, then delete node 10.Step 3: Print remaining list
Starting from new head (20), we print 20 -> 30 -> null.Final Answer:
20 -> 30 -> null -> Option AQuick Check:
First node deleted, list starts at 20 [OK]
- Printing deleted node data
- Not updating head pointer
- Deleting wrong node
- Forgetting to print null at end
void deleteFirstNode(Node* head) {
Node* temp = head;
head = head->next;
delete temp;
}Solution
Step 1: Understand pointer passing in C++
Function receives head by value, so changes inside do not affect original head.Step 2: Identify missing update to original head
Updating local head pointer does not update the caller's head, so list head remains unchanged.Final Answer:
The function does not update the original head pointer outside the function. -> Option CQuick Check:
Pointer passed by value, original head unchanged [OK]
- Assuming head changes outside function
- Deleting node before moving head
- Confusing pointer and value passing
- Ignoring nullptr checks
Solution
Step 1: Pass head by reference to modify original pointer
Using Node*& head allows the function to update the caller's head pointer directly.Step 2: Check for empty list and delete first node safely
Check if head is nullptr to avoid errors, then move head to next node and delete old first node.Final Answer:
void deleteFirstNode(Node*& head) { if (head == nullptr) return; Node* temp = head; head = head->next; delete temp; } -> Option BQuick Check:
Reference parameter updates head safely [OK]
- Passing head by value instead of reference
- Not checking for empty list
- Deleting node without updating head
- Using incorrect pointer syntax
Ready to prove it?
Pass the test and this pattern moves unknown
