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 as the list grows bigger?
Analyze the time complexity of the following code snippet.
void deleteFromBeginning(Node*& head) {
if (head == nullptr) return;
Node* temp = head;
head = head->next;
if (head != nullptr) {
head->prev = nullptr;
}
delete 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 steps |
| 100 | 5 steps |
| 1000 | 5 steps |
Pattern observation: The number of steps stays the same as the list grows.
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 the first node takes longer if the list is bigger because there are more nodes."
[OK] Correct: We only change a few pointers at the start, so the rest of the list size does not affect the time.
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
head pointer when you delete the first node from a doubly linked list?Solution
Step 1: Understand the head pointer role
Theheadpoints to the first node in the doubly linked list.Step 2: After deletion, update the head pointer
When the first node is deleted, theheadmust point 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]
- Not updating head pointer after deletion
- Setting head to null without checking list size
- Leaving head pointing to deleted node
Solution
Step 1: Store current head in temp
We save the current first node in a temporary pointer to delete it safely after updating head.Step 2: Update head and fix previous pointer
Setheadto the next node, then sethead->prevtonullptrto detach the old first node.Step 3: Delete the old first node
Finally, delete the node stored intempto free memory.Final Answer:
Node* temp = head; head = head->next; head->prev = nullptr; delete temp; -> Option BQuick Check:
Proper order: save, update, fix pointers, delete [OK]
- Deleting head before updating pointers
- Not setting head->prev to nullptr
- Accessing head->prev when head is nullptr
struct Node {
int data;
Node* prev;
Node* next;
};
Node* head = new Node{1, nullptr, nullptr};
Node* second = new Node{2, head, nullptr};
head->next = second;
// Delete from beginning
Node* temp = head;
head = head->next;
head->prev = nullptr;
delete temp;
// Print list
Node* curr = head;
while(curr != nullptr) {
std::cout << curr->data << " ";
curr = curr->next;
}Solution
Step 1: Initial list setup
List has two nodes: 1 (head) -> 2. Head points to node with data 1.Step 2: Delete first node and update pointers
Head moves to second node (data 2), and itsprevis set tonullptr. The first node is deleted.Step 3: Print remaining list
Only node with data 2 remains, so output is "2".Final Answer:
2 -> Option DQuick Check:
After deletion, list starts at 2 [OK]
- Printing deleted node's data
- Not updating head->prev to nullptr
- Assuming both nodes print after deletion
void deleteBeginning(Node*& head) {
if (head == nullptr) return;
delete head;
head = head->next;
if (head != nullptr) head->prev = nullptr;
}Solution
Step 1: Analyze deletion order
The code deletesheadfirst, then tries to accesshead->next, which is invalid after deletion.Step 2: Correct order to avoid undefined behavior
First savehead->nextin a temp pointer, updatehead, then delete old node.Final Answer:
Deleting head before updating head pointer causes undefined behavior -> Option AQuick Check:
Accessing deleted memory causes error [OK]
- Deleting node before updating pointers
- Accessing deleted node's members
- Not checking for empty list
Solution
Step 1: Initial list nodes
List is 10 <-> 20 <-> 30 <-> 40, head points to 10.Step 2: First deletion
Delete node 10, head moves to 20, set head->prev = nullptr. List now: 20 <-> 30 <-> 40.Step 3: Second deletion
Delete node 20, head moves to 30, set head->prev = nullptr. List now: 30 <-> 40.Final Answer:
30 <-> 40 -> Option AQuick Check:
Two deletions remove first two nodes [OK]
- Deleting only once instead of twice
- Not updating head->prev after second deletion
- Printing old list without deletions
Ready to prove it?
Pass the test and this pattern moves unknown
