What if you could add anything exactly where you want without breaking your list?
Why Insert at Specific Position in Doubly Linked List in DSA Typescript?
Imagine you have a paper list of your favorite songs. You want to add a new song exactly at position 3, but you have to rewrite the whole list every time you want to insert a song in the middle.
Manually rewriting the list every time you add a song in the middle is slow and easy to mess up. You might lose track of the order or accidentally skip a song.
A doubly linked list lets you jump to the exact spot and insert the new song without rewriting the whole list. It keeps track of neighbors both before and after, so adding anywhere is quick and safe.
let list = ['song1', 'song2', 'song4']; // To insert 'song3' at position 3 list = list.slice(0, 2).concat(['song3'], list.slice(2));
doublyLinkedList.insertAtPosition('song3', 3);
You can quickly and safely add items anywhere in your list without breaking the order or rewriting everything.
Adding a new contact in the middle of your phone's contact list without scrolling through and rewriting the entire list.
Manual insertion in the middle is slow and error-prone.
Doubly linked lists let you insert anywhere efficiently.
This keeps your data organized and easy to update.
Practice
Solution
Step 1: Understand doubly linked list structure
Each node has pointers to both previous and next nodes, so insertion requires updating these pointers.Step 2: Insert node at specific position
We must update the previous node's next pointer and the next node's previous pointer to point to the new node.Final Answer:
Adjusting the previous and next pointers of neighboring nodes to include the new node -> Option AQuick Check:
Pointer updates = Adjust neighbors [OK]
- Only updating one pointer leads to broken links
- Ignoring the previous pointer in insertion
- Replacing nodes instead of inserting
Solution
Step 1: Check for previous and next pointers
A doubly linked list node must have both next and prev pointers, which can be null.Step 2: Verify constructor initializes all properties
class Node { value: number; next: Node | null; prev: Node | null; constructor(value: number) { this.value = value; this.next = null; this.prev = null; } } initializes value, next, and prev correctly with proper types.Final Answer:
class Node { value: number; next: Node | null; prev: Node | null; constructor(value: number) { this.value = value; this.next = null; this.prev = null; } } -> Option DQuick Check:
Both pointers initialized = class Node { value: number; next: Node | null; prev: Node | null; constructor(value: number) { this.value = value; this.next = null; this.prev = null; } } [OK]
- Forgetting to initialize prev pointer
- Using wrong property names
- Not allowing null for next or prev
insertAtPosition(head, 3, 3);Positions start at 1.
Solution
Step 1: Identify position 3 in the list
Positions: 1=1, 2=2, 3=4. We want to insert before node with value 4.Step 2: Insert node with value 3 at position 3
Update pointers so 2 points next to 3, 3 points prev to 2 and next to 4, 4 points prev to 3.Final Answer:
1 <-> 2 <-> 3 <-> 4 -> Option AQuick Check:
Insert at position 3 = 1-2-3-4 [OK]
- Inserting after instead of before position
- Mixing up prev and next pointers
- Off-by-one errors in position counting
function insertAtPosition(head: Node | null, value: number, position: number): Node | null {
let newNode = new Node(value);
if (position === 1) {
newNode.next = head;
if (head) head.prev = newNode;
return newNode;
}
let current = head;
for (let i = 1; i < position - 1; i++) {
if (!current) return head;
current = current.next;
}
newNode.next = current.next;
current.next = newNode;
newNode.prev = current;
return head;
}Solution
Step 1: Analyze pointer updates after insertion
After inserting newNode, newNode.next.prev should point back to newNode to maintain doubly links.Step 2: Identify missing pointer update
The code misses updating newNode.next.prev = newNode, which breaks backward link from next node.Final Answer:
Missing update of newNode.next.prev to newNode -> Option CQuick Check:
Update both next and prev pointers [OK]
- Forgetting to update newNode.next.prev
- Loop off-by-one errors
- Not handling empty list insertion
Solution
Step 1: Identify position 2 in the list
Positions: 1=Engine, 2=Car1. Insert CarX before Car1 to become new position 2.Step 2: Insert CarX at position 2
Update pointers so Engine next points to CarX, CarX prev to Engine, CarX next to Car1, Car1 prev to CarX.Final Answer:
Engine <-> CarX <-> Car1 <-> Car2 <-> Caboose -> Option BQuick Check:
Insert at position 2 = Engine-CarX-Car1... [OK]
- Inserting after instead of before position
- Not updating prev pointers correctly
- Confusing position indexing starting at 0
Ready to prove it?
Pass the test and this pattern moves unknown
