Insert at Specific Position in Doubly Linked List in DSA Javascript - Time & Space Complexity
When inserting a new node at a specific position in a doubly linked list, we want to know how the time needed changes as the list grows.
We ask: How many steps does it take to find the right spot and insert the node?
Analyze the time complexity of the following code snippet.
function insertAtPosition(head, position, data) {
let newNode = { data, prev: null, next: null };
if (position === 1) {
newNode.next = head;
if (head) head.prev = newNode;
return newNode;
}
let current = head;
let count = 1;
while (current && count < position - 1) {
current = current.next;
count++;
}
if (!current) return head; // position out of bounds
newNode.next = current.next;
if (current.next) current.next.prev = newNode;
current.next = newNode;
newNode.prev = current;
return head;
}
This code inserts a new node at the given position by first moving through the list to find the spot, then adjusting pointers to insert.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: The while loop that moves through the list nodes to reach the position before insertion.
- How many times: Up to position - 2 times, which depends on the input position.
As the position increases, the number of steps to reach it grows roughly the same amount.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | Up to 8 steps to reach position 10 |
| 100 | Up to 98 steps to reach position 100 |
| 1000 | Up to 998 steps to reach position 1000 |
Pattern observation: The steps grow linearly with the position number, so doubling the position roughly doubles the work.
Time Complexity: O(n)
This means the time to insert grows directly with the position in the list; farther positions take more time.
[X] Wrong: "Insertion at any position in a doubly linked list always takes constant time O(1)."
[OK] Correct: You must first find the position by moving through the list, which takes time proportional to the position, so it is not always instant.
Understanding how list traversal affects insertion time helps you explain and write efficient code for linked lists, a common topic in interviews.
"What if we kept a pointer to the tail of the list and wanted to insert near the end? How would the time complexity change?"
Practice
Solution
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.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.Final Answer:
The new node becomes the new head of the list. -> Option AQuick Check:
Insert at position 1 = new head [OK]
- Thinking position 1 means end insertion
- Not updating previous pointer of old head
- Ignoring head pointer update
pos in a doubly linked list in JavaScript?Solution
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.Step 2: Update current's next and newNode's prev
Set current.next to newNode and newNode.prev to current to link newNode properly.Final Answer:
newNode.next = current.next; current.next.prev = newNode; current.next = newNode; newNode.prev = current; -> Option AQuick Check:
Correct pointer updates = newNode.next = current.next; current.next.prev = newNode; current.next = newNode; newNode.prev = current; [OK]
- Mixing prev and next pointers
- Not updating current.next.prev
- Incorrect order of assignments causing broken links
Solution
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.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.Final Answer:
10 <-> 15 <-> 20 <-> 30 <-> null -> Option CQuick Check:
Insert at position 2 = between first and second node [OK]
- Inserting at wrong position
- Not updating prev pointer of next node
- Breaking list links after insertion
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?Solution
Step 1: Analyze loop for position traversal
The loop moves current forward pos-2 times. If pos is too large, current becomes null.Step 2: Accessing current.next when current is null
Trying to access current.next.prev causes runtime error because current is null.Final Answer:
Runtime error due to accessing next of null. -> Option BQuick Check:
Out-of-range position causes null access error [OK]
- Not validating position against list length
- Assuming insertion at end without checks
- Ignoring null pointer exceptions
Solution
Step 1: Insert 12 at position 3
Position 3 means between nodes with values 10 and 15. After insertion: 5 <-> 10 <-> 12 <-> 15 <-> 20.Step 2: Remove node at position 4
Position 4 now points to node with value 15. Removing it links 12 directly to 20.Final Answer:
5 <-> 10 <-> 12 <-> 20 <-> null -> Option DQuick Check:
Insert then remove node at position 4 = 5 <-> 10 <-> 12 <-> 20 <-> null [OK]
- Removing wrong node after insertion
- Not updating pointers after removal
- Confusing positions after insertion
Ready to prove it?
Pass the test and this pattern moves unknown
