Bird
Raised Fist0
DSA Javascriptprogramming~10 mins

Insert at Specific Position in Doubly Linked List in DSA Javascript - Execution Trace

Choose your learning style10 modes available
Concept Flow - Insert at Specific Position in Doubly Linked List
Create new node with value
↓
Check if position is 1
↓
Insert at head
↓
Traverse to position-1 node
↓
Adjust pointers: prev, next
↓
Insert node
↓
Done
Create a new node, then either insert at head if position is 1, or traverse to the node before the position and adjust pointers to insert the new node.
Execution Sample
DSA Javascript
function insertAtPosition(head, value, position) {
  let newNode = { val: value, prev: null, next: null };
  if (position === 1) {
    newNode.next = head;
    if (head) head.prev = newNode;
    return newNode;
  }
  let current = head;
  for (let i = 1; i < position - 1 && current; i++) current = current.next;
  if (!current) return head;
  newNode.next = current.next;
  if (current.next) current.next.prev = newNode;
  current.next = newNode;
  newNode.prev = current;
  return head;
}
Inserts a new node with given value at the specified position in a doubly linked list.
Execution Table
StepOperationCurrent NodePointer ChangesVisual State
1Create new node with value=5nullnewNode = {5, prev:null, next:null}null
2Check if position=1head=1No pointer change1 <-> 2 <-> 3 <-> null
3Traverse to position-1 nodecurrent=1No pointer change1 <-> 2 <-> 3 <-> null
4Traverse to next nodecurrent=2No pointer change1 <-> 2 <-> 3 <-> null
5Set newNode.next = current.next (3)current=2newNode.next = 31 <-> 2 <-> 3 <-> null
6Set current.next.prev = newNodecurrent=23.prev = newNode1 <-> 2 <-> 3 <-> null
7Set current.next = newNodecurrent=22.next = newNode1 <-> 2 <-> 5 <-> 3 <-> null
8Set newNode.prev = currentcurrent=2newNode.prev = 21 <-> 2 <-> 5 <-> 3 <-> null
9Return headhead=1No pointer change1 <-> 2 <-> 5 <-> 3 <-> null
💡 Insertion complete at position 3; list updated with new node 5.
Variable Tracker
VariableStartAfter Step 1After Step 3After Step 5After Step 7Final
head1 <-> 2 <-> 3 <-> null1 <-> 2 <-> 3 <-> null1 <-> 2 <-> 3 <-> null1 <-> 2 <-> 3 <-> null1 <-> 2 <-> 5 <-> 3 <-> null1 <-> 2 <-> 5 <-> 3 <-> null
currentnullnull1222
newNodenull{5, prev:null, next:null}{5, prev:null, next:null}{5, prev:null, next:3}{5, prev:null, next:3}{5, prev:2, next:3}
Key Moments - 3 Insights
Why do we traverse to position-1 node before inserting?
Because we need to adjust pointers of the node before the insertion point to link the new node correctly, as shown in steps 3-7 in the execution table.
What happens if position is 1?
The new node becomes the new head, its next points to old head, and old head's prev points back to new node, as shown in the concept flow and handled in code step 2.
Why do we check if current.next exists before setting current.next.prev?
To avoid errors when inserting at the end of the list where current.next is null, so we only update prev pointer if next node exists, as in step 6.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the value of 'current' after step 4?
ANode with value 1
BNode with value 3
CNode with value 2
Dnull
💡 Hint
Check the 'Current Node' column at step 4 in the execution table.
At which step is the new node's prev pointer set?
AStep 6
BStep 8
CStep 7
DStep 5
💡 Hint
Look at the 'Pointer Changes' column to find when newNode.prev is assigned.
If we insert at position 1, what will be the new head's next node?
AOld head node
Bnull
CNew node itself
DSecond node in original list
💡 Hint
Refer to the concept flow and code where position === 1 is handled.
Concept Snapshot
Insert at Specific Position in Doubly Linked List:
- Create new node
- If position=1, insert at head
- Else traverse to position-1 node
- Adjust pointers: newNode.next, newNode.prev, current.next, current.next.prev
- Return head
- Handles insertion at middle or end
- Maintains bidirectional links
Full Transcript
To insert a node at a specific position in a doubly linked list, first create the new node. If the position is 1, insert the new node at the head by linking it before the current head. Otherwise, traverse the list to the node just before the desired position. Then adjust the pointers: set the new node's next to current's next, set the next node's prev to new node if it exists, set current's next to new node, and set new node's prev to current. Finally, return the head of the list. This process ensures the doubly linked list remains properly linked in both directions after insertion.

Practice

(1/5)
1. What happens when you insert a new node at position 1 in a doubly linked list?
easy
A. The new node becomes the new head of the list.
B. The new node is added at the end of the list.
C. The new node replaces the last node.
D. The list remains unchanged.

Solution

  1. Step 1: Understand position 1 in doubly linked list

    Position 1 means the start of the list, so the new node should be the first node.
  2. Step 2: Effect of insertion at position 1

    The new node's next pointer points to the old head, and the old head's previous pointer points back to the new node. The new node becomes the head.
  3. Final Answer:

    The new node becomes the new head of the list. -> Option A
  4. Quick Check:

    Insert at position 1 = new head [OK]
Hint: Position 1 means new head insertion [OK]
Common Mistakes:
  • Thinking position 1 means end insertion
  • Not updating previous pointer of old head
  • Ignoring head pointer update
2. Which of the following is the correct syntax to insert a new node at position pos in a doubly linked list in JavaScript?
easy
A. newNode.next = current.next; current.next.prev = newNode; current.next = newNode; newNode.prev = current;
B. newNode.prev = current.next; current.prev = newNode; newNode.next = current; current.next = newNode;
C. newNode.next = current.prev; current.next = newNode; newNode.prev = current.next;
D. newNode.prev = current; current.next = newNode; newNode.next = current.next; current.next.prev = newNode;

Solution

  1. Step 1: Identify correct pointer updates for insertion

    When inserting after current, newNode.next should point to current.next, and current.next.prev should point back to newNode.
  2. Step 2: Update current's next and newNode's prev

    Set current.next to newNode and newNode.prev to current to link newNode properly.
  3. Final Answer:

    newNode.next = current.next; current.next.prev = newNode; current.next = newNode; newNode.prev = current; -> Option A
  4. Quick Check:

    Correct pointer updates = newNode.next = current.next; current.next.prev = newNode; current.next = newNode; newNode.prev = current; [OK]
Hint: Next and prev pointers must be updated symmetrically [OK]
Common Mistakes:
  • Mixing prev and next pointers
  • Not updating current.next.prev
  • Incorrect order of assignments causing broken links
3. Given the doubly linked list: 10 <-> 20 <-> 30 <-> null, what is the list after inserting 15 at position 2 using correct insertion logic?
medium
A. 10 <-> 20 <-> 30 <-> 15 <-> null
B. 15 <-> 10 <-> 20 <-> 30 <-> null
C. 10 <-> 15 <-> 20 <-> 30 <-> null
D. 10 <-> 20 <-> 15 <-> 30 <-> null

Solution

  1. Step 1: Identify position 2 in the list

    Position 2 means after the first node (10), so 15 should be inserted between 10 and 20.
  2. Step 2: Insert 15 between 10 and 20

    Update pointers so 10 points to 15, 15 points to 20, and 20's prev points to 15.
  3. Final Answer:

    10 <-> 15 <-> 20 <-> 30 <-> null -> Option C
  4. Quick Check:

    Insert at position 2 = between first and second node [OK]
Hint: Position 2 means after first node [OK]
Common Mistakes:
  • Inserting at wrong position
  • Not updating prev pointer of next node
  • Breaking list links after insertion
4. Consider this code snippet to insert a node at position 3 in a doubly linked list:
let current = head;
for(let i = 1; i < pos - 1; i++) {
  current = current.next;
}
newNode.next = current.next;
current.next.prev = newNode;
current.next = newNode;
newNode.prev = current;
What error might occur if pos is greater than the list length?
medium
A. The new node is inserted at the head incorrectly.
B. Runtime error due to accessing next of null.
C. The list becomes circular causing infinite loop.
D. No error; insertion happens at the end automatically.

Solution

  1. Step 1: Analyze loop for position traversal

    The loop moves current forward pos-2 times. If pos is too large, current becomes null.
  2. Step 2: Accessing current.next when current is null

    Trying to access current.next.prev causes runtime error because current is null.
  3. Final Answer:

    Runtime error due to accessing next of null. -> Option B
  4. Quick Check:

    Out-of-range position causes null access error [OK]
Hint: Check list length before insertion [OK]
Common Mistakes:
  • Not validating position against list length
  • Assuming insertion at end without checks
  • Ignoring null pointer exceptions
5. You have a doubly linked list: 5 <-> 10 <-> 15 <-> 20 <-> null. You want to insert a new node with value 12 at position 3. After insertion, you also want to remove the node at position 4. What will be the final list?
hard
A. 5 <-> 12 <-> 10 <-> 20 <-> null
B. 5 <-> 10 <-> 12 <-> 15 <-> 20 <-> null
C. 5 <-> 10 <-> 15 <-> 20 <-> null
D. 5 <-> 10 <-> 12 <-> 20 <-> null

Solution

  1. Step 1: Insert 12 at position 3

    Position 3 means between nodes with values 10 and 15. After insertion: 5 <-> 10 <-> 12 <-> 15 <-> 20.
  2. Step 2: Remove node at position 4

    Position 4 now points to node with value 15. Removing it links 12 directly to 20.
  3. Final Answer:

    5 <-> 10 <-> 12 <-> 20 <-> null -> Option D
  4. Quick Check:

    Insert then remove node at position 4 = 5 <-> 10 <-> 12 <-> 20 <-> null [OK]
Hint: Insert first, then remove target node [OK]
Common Mistakes:
  • Removing wrong node after insertion
  • Not updating pointers after removal
  • Confusing positions after insertion
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard