Delete from Beginning of Doubly Linked List in DSA Typescript - 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 {
value: number;
next: Node | null = null;
prev: Node | null = null;
constructor(value: number) { this.value = value; }
}
function deleteFromBeginning(head: Node | null): Node | null {
if (!head) return null;
const 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 a few pointers (head, head.next, head.next.prev).
- 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 | 2-3 pointer updates |
| 100 | 2-3 pointer updates |
| 1000 | 2-3 pointer updates |
Pattern observation: The number of steps stays the same regardless of list size.
Time Complexity: O(1)
This means deleting from the start takes a fixed amount of time no matter how long 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, so the list size does not affect the time.
Knowing this helps you explain why some operations are quick even on big lists, showing you understand data structure efficiency.
"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 role of head pointer
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, head must point to the next node to keep the list accessible.Final Answer:
It moves to the next node in the list -> Option DQuick Check:
head points to next node after deletion [OK]
- Setting head to null without checking if list is empty
- Not updating head after deletion
- Pointing head to deleted node
head is the pointer to the first node?Solution
Step 1: Move head to next node
Assign head to head.next to remove first node reference.Step 2: Set new head's prev to null
Set head.prev to null to disconnect backward link from old first node.Final Answer:
head = head.next; head.prev = null; -> Option AQuick Check:
Correct syntax to update head and prev pointer [OK]
- Setting head.prev before moving head
- Assigning head.prev to null before head changes
- Setting head.next to null incorrectly
Solution
Step 1: Identify the first node
The first node is 10, which will be removed.Step 2: Update head to next node
Head moves to 20, and 20.prev is set to null.Final Answer:
20 <-> 30 <-> null -> Option AQuick Check:
First node removed, list starts at 20 [OK]
- Not updating prev pointer of new head
- Removing wrong node
- Leaving old head in list
if (head !== null) {
head = head.next;
head.prev = null;
}
What error can occur here?Solution
Step 1: Check condition when list has one node
If list has one node, head.next is null after deletion.Step 2: Accessing head.prev causes error
head becomes null, so head.prev causes runtime error (cannot access property of null).Final Answer:
Runtime error if list has only one node -> Option CQuick Check:
Null pointer access error possible [OK]
- Not checking if head is null after moving to next
- Assuming list always has multiple nodes
- Ignoring runtime null errors
Solution
Step 1: Check if list is empty
First, verify head is not null to avoid errors on empty list.Step 2: Move head to next node and check again
After moving head, check if new head is not null before setting prev to null.Final Answer:
if (head !== null) { head = head.next; if (head !== null) head.prev = null; } -> Option BQuick Check:
Safe deletion with null checks [OK]
- Not checking if head is null after moving
- Setting prev pointer without null check
- Assuming list has multiple nodes always
Ready to prove it?
Pass the test and this pattern moves unknown
