Delete from Beginning of Doubly Linked List in DSA C - Time & Space Complexity
We want to understand how long it takes to remove the first item from a doubly linked list.
How does the time needed change when the list grows bigger?
Analyze the time complexity of the following code snippet.
// Delete node from beginning of doubly linked list
void deleteBeginning(Node** head) {
if (*head == NULL) return;
Node* temp = *head;
*head = (*head)->next;
if (*head != NULL) {
(*head)->prev = NULL;
}
free(temp);
}
This code removes the first node from a doubly linked list and updates pointers accordingly.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Single pointer update and node deletion.
- How many times: Exactly once per call, no loops or recursion.
Removing the first node always takes the same steps, no matter how big the list is.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 5-6 steps |
| 100 | 5-6 steps |
| 1000 | 5-6 steps |
Pattern observation: The number of steps stays the same regardless of list size.
Time Complexity: O(1)
This means deleting the first node takes a constant amount of time no matter how large the list is.
[X] Wrong: "Deleting from the beginning takes longer as the list grows because we have to move through nodes."
[OK] Correct: We only change a few pointers at the start; we do not traverse the list, so time does not grow with list size.
Knowing that deleting from the start of a doubly linked list is quick helps you explain efficient list operations clearly in interviews.
"What if we changed this to delete from the end of the doubly linked list? 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 doubly linked list.Step 2: Effect of deleting the first node
When the first node is deleted, the head pointer must move to the next node, which becomes the new first node.Final Answer:
It moves to the second node in the list -> Option CQuick Check:
Head points to new first node after deletion [OK]
- Assuming head becomes NULL even if list has more nodes
- Not updating head pointer after deletion
- Pointing head to last node mistakenly
Solution
Step 1: Store current head in temp
We save the current first 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 second node, free the old first node, and set new head's prev to NULL.Final Answer:
temp = head; head = head->next; free(temp); head->prev = NULL; -> Option AQuick Check:
Proper order: save, move, free, update pointers [OK]
- Freeing head before moving pointer
- Not setting new head's prev to NULL
- Accessing freed memory after free
Solution
Step 1: Identify the first node to delete
The first node has value 10, which will be removed.Step 2: Update head to second node and adjust pointers
Head moves to node with value 20, and its prev pointer is set to NULL.Final Answer:
20 <-> 30 <-> NULL -> Option AQuick Check:
First node removed, list starts at 20 [OK]
- Leaving old head in list
- Not updating prev pointer of new head
- Deleting wrong node
void deleteBegin() {
if (head == NULL) return;
free(head);
head = head->next;
if (head != NULL) head->prev = NULL;
}Solution
Step 1: Analyze order of operations
The code frees head first, then tries to access head->next, which is invalid after free.Step 2: Correct order needed
We must first save head->next in a temp pointer, move head, then free old node to avoid invalid memory access.Final Answer:
It frees head before moving pointer, causing invalid access -> Option DQuick Check:
Free after pointer update to avoid invalid access [OK]
- Freeing node before updating pointers
- Accessing freed memory
- Not handling empty list properly
Solution
Step 1: Delete first node once
First deletion removes node with value 5, head moves to 15.Step 2: Delete first node again
Second deletion removes node with value 15, head moves to 25.Final Answer:
25 <-> 35 <-> NULL -> Option BQuick Check:
Two deletions remove first two nodes [OK]
- Deleting only once instead of twice
- Not updating head correctly after second deletion
- Confusing node values after deletion
Ready to prove it?
Pass the test and this pattern moves unknown
