Delete Node at Beginning in DSA C++ - Time & Space Complexity
We want to understand how long it takes to remove the first node from a linked list.
How does the time needed change as the list gets bigger?
Analyze the time complexity of the following code snippet.
void deleteAtBeginning(Node*& head) {
if (head == nullptr) return;
Node* temp = head;
head = head->next;
delete temp;
}
This code removes the first node from a linked list by moving the head pointer to the next node and deleting the old first node.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Single pointer update and delete operation.
- How many times: Exactly once per call, no loops or recursion.
The time to delete the first node stays the same no matter how big the list is.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 4 operations |
| 100 | 4 operations |
| 1000 | 4 operations |
Pattern observation: The number of steps does not increase as the list grows.
Time Complexity: O(1)
This means deleting the first node takes the same amount of time no matter how large the list is.
[X] Wrong: "Deleting the first node takes longer if the list is bigger because we have to check all nodes."
[OK] Correct: We only change the head pointer and delete one node, so the list size does not affect the time.
Knowing that deleting the first node is quick helps you explain efficient list operations clearly and confidently.
"What if we changed this to delete the last node instead? How would the time complexity change?"
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
