Delete from End of Doubly Linked List in DSA Javascript - Time & Space Complexity
When we delete a node from the end of a doubly linked list, we want to know how the time it takes changes as the list grows.
We ask: How many steps does it take to remove the last node when the list gets bigger?
Analyze the time complexity of the following code snippet.
class Node {
constructor(data) {
this.data = data;
this.prev = null;
this.next = null;
}
}
class DoublyLinkedList {
constructor() {
this.head = null;
this.tail = null;
}
deleteFromEnd() {
if (!this.tail) return null;
const removed = this.tail;
this.tail = this.tail.prev;
if (this.tail) this.tail.next = null;
else this.head = null;
return removed.data;
}
}
This code removes the last node from a doubly linked list by updating pointers.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Accessing the tail node and updating pointers.
- How many times: This happens once per deletion, no loops or recursion involved.
Deleting from the end always takes the same number of 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 as the list grows.
Time Complexity: O(1)
This means deleting from the end takes a constant amount of time regardless of list size.
[X] Wrong: "Deleting from the end takes longer as the list grows because you have to find the last node."
[OK] Correct: The doubly linked list keeps a direct reference to the last node (tail), so no searching is needed.
Knowing that deleting from the end of a doubly linked list is quick shows you understand how pointers help save time in data structures.
"What if we did not keep a tail pointer? How would the time complexity change when deleting from the end?"
Practice
tail pointer when you delete the last node from a doubly linked list?Solution
Step 1: Understand tail pointer role
The tail pointer always points to the last node in the list.Step 2: Effect of deleting last node
When the last node is deleted, tail must move to the previous node to remain correct.Final Answer:
It moves to the previous node of the deleted node. -> Option CQuick Check:
Tail updates to previous node [OK]
- Setting tail to null without checking list emptiness
- Not updating tail pointer after deletion
- Moving tail to next node which doesn't exist
Solution
Step 1: Move tail pointer to previous node
To delete last node, tail moves to tail.prev.Step 2: Set new tail's next to null
New tail's next must be null to mark end of list.Final Answer:
tail = tail.prev; tail.next = null; -> Option AQuick Check:
Correct tail update and next nullify [OK]
- Moving tail forward instead of backward
- Not setting tail.next to null
- Setting tail.prev to null incorrectly
1 <-> 2 <-> 3 <-> 4. After deleting the last node, what will be the new list?Solution
Step 1: Identify last node
Last node is 4, which will be removed.Step 2: Remove last node and update tail
Tail moves to node 3, and node 3's next becomes null.Final Answer:
1 <-> 2 <-> 3 -> Option BQuick Check:
Last node removed, list ends at 3 [OK]
- Forgetting to update tail pointer
- Not setting new tail's next to null
- Removing wrong node
if (tail === null) return; tail = tail.prev; tail.next = null;
Solution
Step 1: Check single node case
If list has one node, tail.prev is null, so tail becomes null.Step 2: Problem with tail.next assignment
After tail becomes null, tail.next causes error because tail is null.Final Answer:
It does not handle the case when list has only one node. -> Option DQuick Check:
Single node deletion needs special handling [OK]
- Not checking if tail is null after update
- Accessing properties of null tail
- Ignoring empty list case
10 <-> 20 <-> 30. After deleting the last node twice, what will be the state of the list?Solution
Step 1: Delete last node once
Last node 30 is removed, list becomes 10 <-> 20.Step 2: Delete last node again
Now last node 20 is removed, list becomes 10 only.Step 3: Verify final list
After two deletions, list is just node 10.Final Answer:
10 -> Option AQuick Check:
Two deletions remove last two nodes [OK]
- Assuming list becomes empty after two deletions
- Not updating tail pointer correctly
- Confusing node values after deletion
Ready to prove it?
Pass the test and this pattern moves unknown
