Delete Node at Beginning in DSA C - Time & Space Complexity
We want to know how the time needed changes when we delete the first node in a linked list.
How does the work grow as the list gets bigger?
Analyze the time complexity of the following code snippet.
void deleteAtBeginning(Node** head) {
if (*head == NULL) return;
Node* temp = *head;
*head = (*head)->next;
free(temp);
}
This code removes the first node from a linked list by moving the head pointer to the next node and freeing the old first node.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Accessing and updating the head pointer once.
- How many times: Exactly once per deletion.
Deleting the first node always takes the same steps no matter how big the list is.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 4 steps |
| 100 | 4 steps |
| 1000 | 4 steps |
Pattern observation: The work stays the same even if the list grows larger.
Time Complexity: O(1)
This means deleting the first node takes a fixed amount of time no matter how big 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 free one node; we do not look at other nodes.
Understanding this helps you explain how simple operations on linked lists work efficiently, a key skill in many coding tasks.
"What if we had 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 linked list.Step 2: Effect of deleting the first node
Deleting the first node means moving the head pointer to the second node, effectively removing the first node from the list.Final Answer:
The head pointer moves to the next node, removing the first node. -> Option DQuick Check:
Deleting at beginning means head moves forward [OK]
- Thinking the last node is deleted
- Assuming the list stays the same
- Deleting all nodes instead of one
Solution
Step 1: Store current head in temp
We save the current head node in a temporary pointer to free it later.Step 2: Move head to next node and free old head
Update head to point to the next node, then free the old head stored in temp.Final Answer:
temp = head; head = head->next; free(temp); -> Option AQuick Check:
Save old head, move head, free old [OK]
- Freeing head before moving pointer
- Not saving old head before moving
- Setting next to NULL incorrectly
Solution
Step 1: Identify the first node
The first node has value 10.Step 2: Delete first node and move head
After deletion, head points to node with value 20, so list starts from 20.Final Answer:
20 -> 30 -> NULL -> Option CQuick Check:
First node removed, next node becomes head [OK]
- Not updating head pointer
- Deleting wrong node
- Assuming list becomes empty
struct Node* temp = head; head = head->next; free(head);What is the error in this code?
Solution
Step 1: Analyze pointer usage
temp stores old head, head moves to next node.Step 2: Identify which node is freed
free(head) frees the new head, not the old node stored in temp, causing memory error.Final Answer:
It frees the new head instead of the old head. -> Option AQuick Check:
Free old node, not new head [OK]
- Freeing updated head instead of old node
- Not freeing any node causing memory leak
- Confusing temp and head pointers
Solution
Step 1: Delete first node once
First deletion removes 5, list becomes 15 -> 25 -> 35 -> NULL.Step 2: Delete first node again
Second deletion removes 15, list becomes 25 -> 35 -> NULL.Final Answer:
25 -> 35 -> NULL -> Option BQuick Check:
Two deletions remove first two nodes [OK]
- Deleting only once instead of twice
- Confusing node values after deletion
- Assuming list becomes empty too soon
Ready to prove it?
Pass the test and this pattern moves unknown
