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.
Specifically, how the time needed changes as the list grows larger.
Analyze the time complexity of the following code snippet.
public void DeleteFromBeginning()
{
if (head == null) return; // empty list
head = head.Next;
if (head != null)
head.Prev = null;
}
This code removes the first node from a doubly linked list by updating the head pointer and adjusting links.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Updating pointers of the first node only.
- How many times: Exactly once per call, no loops or recursion.
Removing the first node always takes the same number of steps, no matter how big the list is.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 3 steps |
| 100 | 3 steps |
| 1000 | 3 steps |
Pattern observation: The time stays constant as the list grows.
Time Complexity: O(1)
This means deleting from the beginning takes the same short 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 a few pointers at the start, no need to look at other nodes.
Knowing this operation is quick helps you explain efficient list manipulations clearly and confidently.
"What if we changed to deleting 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 role of head pointer
The head pointer always 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 DQuick 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: Move head to next node
To delete the first node, assign head to head.next to point to the second node.Step 2: Remove backward link
Set head.prev to null to disconnect the previous first node.Final Answer:
head = head.next; head.prev = null; -> Option AQuick Check:
Correct syntax updates head and prev pointer [OK]
- Setting head.prev instead of head.next
- Not nullifying prev pointer of new head
- Setting head to null prematurely
Solution
Step 1: Identify the first node
The first node has value 10, which will be removed.Step 2: Update head to second node
Head moves to node with value 20, and its prev pointer is set to null.Final Answer:
20 <-> 30 <-> null -> Option BQuick Check:
First node removed, list starts at 20 [OK]
- Removing wrong node (like 20 or 30)
- Not updating prev pointer of new head
- Leaving old head in the list
if (head != null) {
head = head.next;
head.prev = null;
}
What error can occur here?Solution
Step 1: Analyze when list has one node
If list has one node, head.next is null after assignment.Step 2: Accessing head.prev causes error
Setting head.prev = null when head is null causes NullReferenceException.Final Answer:
NullReferenceException if list has only one node -> Option CQuick Check:
Accessing property on null causes exception [OK]
- Not checking if head is null after moving to next
- Assuming code always works for single-node list
- Ignoring runtime exceptions
Solution
Step 1: Delete first node once
First deletion removes node with value 5; head moves to 15.Step 2: Delete first node second time
Second deletion removes node with value 15; head moves to 25.Final Answer:
25 <-> 35 <-> null -> Option AQuick Check:
Two deletions remove first two nodes [OK]
- Deleting only once instead of twice
- Not updating head correctly after second deletion
- Confusing node values order
Ready to prove it?
Pass the test and this pattern moves unknown
