Delete from Beginning of Doubly Linked List in DSA Java - 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 larger?
Analyze the time complexity of the following code snippet.
class DoublyLinkedList {
Node head;
void deleteFromBeginning() {
if (head == null) return;
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 the head pointer and adjusting one or two links.
- How many times: This happens only once per call, no loops or recursion.
Removing the first node always takes the same small number of steps, no matter how big the list is.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 3-4 steps |
| 100 | 3-4 steps |
| 1000 | 3-4 steps |
Pattern observation: The time stays the same no matter how large the list grows.
Time Complexity: O(1)
This means deleting the first node takes a fixed amount of time regardless of list size.
[X] Wrong: "Deleting from the beginning takes longer as the list grows because we have to move through the list."
[OK] Correct: We only change a few pointers at the start; we do not traverse the list at all.
Knowing that deleting from the start of a doubly linked list is quick shows you understand how linked lists work and pointer updates.
"What if we had to delete the last node instead? 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
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 AQuick Check:
Deleting first node updates head to next node [OK]
- Not updating head pointer after deletion
- Setting head to null even if list has more nodes
- Confusing head with tail pointer
prev pointer of the new head node after deleting the first node in a doubly linked list?Solution
Step 1: Identify the new head node
After deleting the first node, the new head is the second node in the list.Step 2: Set the previous pointer of new head to null
Since the new head is the first node now, itsprevpointer must be null to indicate no node before it.Final Answer:
head.prev = null; -> Option CQuick Check:
New head prev pointer must be null [OK]
- Setting head.next to null instead of head.prev
- Using invalid syntax like null.prev
- Trying to access prev.next which may cause error
if (head == null) return; head = head.next; if (head != null) head.prev = null;
What will be the state of the list after deleting the first node from the list
1 <-> 2 <-> 3?Solution
Step 1: Initial list and deletion
The list is 1 <-> 2 <-> 3. Deleting the first node means removing node with value 1.Step 2: Update head and prev pointer
Head moves to node 2. Then head.prev is set to null, so node 2 has no previous node.Final Answer:
2 <-> 3 -> Option DQuick Check:
First node removed, head points to second node [OK]
- Not setting head.prev to null causing broken links
- Assuming node 1 still exists
- Confusing next and prev pointers
if (head == null) return; head = head.next; head.prev = null;
What is the error and how to fix it?
Solution
Step 1: Identify cause of NullPointerException
If the list has only one node, after head = head.next, head becomes null. Then head.prev causes NullPointerException.Step 2: Fix by checking head before accessing prev
Add a condition to check if head is not null before setting head.prev = null.Final Answer:
NullPointerException when list has one node; fix by checking if head is null before setting head.prev -> Option BQuick Check:
Always check for null before accessing object members [OK]
- Not checking for null causing runtime errors
- Assuming list always has multiple nodes
- Ignoring exception details
10 <-> 20 <-> 30 <-> 40. After deleting the first node twice using the correct deletion method, what will be the printed list?Solution
Step 1: First deletion
Deleting first node removes 10. Head moves to 20, head.prev set to null. List becomes 20 <-> 30 <-> 40.Step 2: Second deletion
Deleting first node again removes 20. Head moves to 30, head.prev set to null. List becomes 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 changes
Ready to prove it?
Pass the test and this pattern moves unknown
