What if removing the first item from a list could be as simple as snapping your fingers?
Why Delete from Beginning of Doubly Linked List in DSA Typescript?
Imagine you have a long chain of paper clips linked together. You want to remove the first paper clip from the chain. Doing this by hand means you have to carefully unhook the first clip without breaking the chain or losing track of the rest.
Manually removing the first link can be slow and risky. You might accidentally break the chain or lose the connection to the rest of the clips. If the chain is very long, it becomes hard to keep track of all the links and remove the first one safely.
A doubly linked list is like a chain where each link knows both its previous and next neighbors. Deleting the first link is easy and safe because you just update the pointers of the second link to become the new start. This way, the chain stays connected without any breaks.
let head = firstNode; head = head.next; // manually move head head.prev = null; // manually fix previous pointer
function deleteFromBeginning() {
if (!this.head) return;
this.head = this.head.next;
if (this.head) this.head.prev = null;
}This operation lets you quickly and safely remove the first item from a list, keeping the rest perfectly connected and ready for more changes.
Think of a playlist where you remove the first song after it finishes playing. The next song automatically becomes the new start without breaking the order.
Manually removing the first item is error-prone and slow.
Doubly linked lists keep track of neighbors, making removal easy.
Deleting from the beginning updates pointers to keep the list connected.
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
