Delete from Beginning of Doubly Linked List in DSA Javascript - 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.
class Node {
constructor(value) {
this.value = value;
this.next = null;
this.prev = null;
}
}
function deleteFromBeginning(head) {
if (!head) return null;
let newHead = head.next;
if (newHead) newHead.prev = null;
return newHead;
}
This code removes the first node from a doubly linked list and returns the new head.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Accessing and updating the first two nodes' pointers.
- How many times: This happens only once, 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 | 3-4 steps |
| 100 | 3-4 steps |
| 1000 | 3-4 steps |
Pattern observation: The number of steps stays the same no matter how large the list is.
Time Complexity: O(1)
This means deleting the first node takes a constant amount of time regardless of list size.
[X] Wrong: "Deleting the first node takes longer as the list grows because we have to check all nodes."
[OK] Correct: We only update the first two nodes' pointers, so 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 had to delete the last node instead? How would the time complexity change?"
Practice
Solution
Step 1: Understand the head pointer role
The head pointer always points to the first node in the 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 BQuick Check:
Head points to new first node after deletion [OK]
- Assuming head becomes null without checking list length
- Not updating head pointer after deletion
- Pointing head to last node mistakenly
Solution
Step 1: Move head to next node
Assign head to head.next to remove the first node reference.Step 2: Update new head's prev pointer
Set head.prev to null to disconnect backward link from old first node.Final Answer:
head = head.next; head.prev = null; -> Option DQuick Check:
Correct syntax updates head and prev pointer [OK]
- Setting head.prev before moving head
- Setting head.next to null incorrectly
- Nullifying head before updating pointers
Solution
Step 1: Identify the first node
The first node is 10, which will be removed.Step 2: Update head to second node
Head moves to 20, and 20.prev is set to null.Final Answer:
20 <-> 30 <-> null -> Option CQuick Check:
First node removed, list starts at 20 [OK]
- Leaving old head in list
- Not updating prev pointer of new head
- Removing wrong node
if (head !== null) {
head = head.next;
head.prev = null;
}Solution
Step 1: Check list with one node
If list has one node, head.next is null after head = head.next.Step 2: Accessing head.prev causes error
head is now null, so head.prev causes runtime error (cannot read property 'prev' of null).Final Answer:
head.prev assignment causes error if list has one node -> Option AQuick Check:
Null head causes error on prev access [OK]
- Not handling single-node list case
- Accessing properties of null head
- Assuming list always has multiple nodes
Solution
Step 1: Move head to next node
Head moves from 5 to 15, the second node.Step 2: Set new head's prev to null
Set 15.prev = null to disconnect from old first node.Step 3: Return updated head
Return the new head pointing to 15.Final Answer:
15 <-> 25 <-> 35 <-> null -> Option AQuick Check:
First node removed, list starts at 15 [OK]
- Not updating prev pointer of new head
- Returning old head instead of new head
- Deleting wrong node or skipping nodes
Ready to prove it?
Pass the test and this pattern moves unknown
