What if you could add new things to the start of your list instantly without rewriting everything?
Why Insert at Beginning of Doubly Linked List in DSA Javascript?
Imagine you have a list of your favorite songs written on paper. You want to add a new favorite song right at the top of the list every time you discover one. Doing this by rewriting the whole list every time is tiring and slow.
Manually rewriting the entire list to add a new song at the beginning takes a lot of time and can cause mistakes like skipping songs or mixing the order. It's hard to keep track and update the list quickly.
Using a doubly linked list lets you add a new item at the start easily by just changing a few links. You don't have to rewrite the whole list, and you can move forward or backward through the list smoothly.
let songs = ['Song1', 'Song2', 'Song3']; songs = ['NewSong', ...songs];
class Node {
constructor(data) {
this.data = data;
this.next = null;
this.prev = null;
}
}
// Insert new node at beginning by adjusting pointersThis lets you quickly add new items at the start and navigate both forward and backward through your list without hassle.
Think of a browser history where you want to add the latest visited page at the front and be able to go back and forth easily.
Manually adding at the start is slow and error-prone.
Doubly linked lists let you insert at the beginning by changing just a few links.
This makes adding and navigating items fast and easy.
Practice
Solution
Step 1: Understand the head node position
The head node is the first node in the doubly linked list.Step 2: Insert new node before head
Inserting at the beginning means placing the new node before the current head, making it the new head.Final Answer:
To add a new node before the current head node -> Option AQuick Check:
Insert at beginning = add before head [OK]
- Confusing insertion at beginning with insertion at end
- Thinking insertion removes nodes
- Mixing up head and tail nodes
Solution
Step 1: Identify the old head's previous pointer
The old head's previous pointer should point to the new node after insertion.Step 2: Update old head's prev to new node
Setting oldHead.prev = newNode links the old head back to the new node.Final Answer:
oldHead.prev = newNode; -> Option AQuick Check:
Old head prev points to new node [OK]
- Setting oldHead.next instead of prev
- Confusing newNode's pointers
- Assigning newNode.next incorrectly
Solution
Step 1: Insert 10 before current head (5)
The new node with value 10 becomes the new head, pointing next to 5.Step 2: Link the rest of the list after 5
The list continues as 5 <-> 15 <-> 20 unchanged after insertion.Final Answer:
10 <-> 5 <-> 15 <-> 20 -> Option CQuick Check:
New head 10 followed by old list [OK]
- Appending new node at end instead of beginning
- Swapping nodes incorrectly
- Ignoring pointer updates
function insertAtBeginning(head, data) {
const newNode = { data: data, prev: null, next: head };
head.prev = newNode;
head = newNode;
return head;
}Solution
Step 1: Check for empty list case
If head is null, head.prev causes error because head is null.Step 2: Missing null check before setting head.prev
Code should check if head exists before setting head.prev = newNode.Final Answer:
It does not handle empty list when head is null -> Option BQuick Check:
Null head requires special handling [OK]
- Ignoring empty list case
- Assuming head is never null
- Not returning updated head
Solution
Step 1: Insert 3 before current head 7
The new node 3 becomes the head, pointing next to 7.Step 2: Traverse forward and backward
Forward traversal: 3 -> 7 -> 14 -> 21; backward traversal reverses this order.Final Answer:
Head to tail: 3 <-> 7 <-> 14 <-> 21; Tail to head: 21 <-> 14 <-> 7 <-> 3 -> Option DQuick Check:
New head first, tail last, pointers reversed backward [OK]
- Mixing forward and backward traversal order
- Appending new node at tail instead of head
- Incorrect pointer updates causing wrong traversal
Ready to prove it?
Pass the test and this pattern moves unknown
