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?"
