Insert at Specific Position in Doubly Linked List in DSA Java - Time & Space Complexity
We want to understand how long it takes to insert a new item at a certain place in a doubly linked list.
Specifically, how the time changes when the list gets bigger.
Analyze the time complexity of the following code snippet.
public void insertAtPosition(int data, int position) {
Node newNode = new Node(data);
if (position == 1) {
newNode.next = head;
if (head != null) head.prev = newNode;
head = newNode;
return;
}
Node current = head;
for (int i = 1; i < position - 1 && current != null; i++) {
current = current.next;
}
if (current == null) return; // position out of bounds
newNode.next = current.next;
if (current.next != null) current.next.prev = newNode;
current.next = newNode;
newNode.prev = current;
}
This code inserts a new node at a given position in a doubly linked list by moving to that position and adjusting links.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: The for-loop that moves through the list nodes to reach the position.
- How many times: Up to (position - 2) times, which depends on input position.
As the list grows, the time to reach the position grows roughly in a straight line with the position number.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | Up to 9 steps to reach position |
| 100 | Up to 99 steps to reach position |
| 1000 | Up to 999 steps to reach position |
Pattern observation: The time grows directly with the position number, so doubling the position roughly doubles the steps.
Time Complexity: O(n)
This means the time to insert grows linearly with the position where we insert in the list.
[X] Wrong: "Insertion at any position in a doubly linked list always takes constant time O(1)."
[OK] Correct: We must first find the position by moving through the list, which takes time proportional to the position, so it is not always instant.
Knowing how insertion time depends on position helps you explain and choose the right data structure for tasks needing fast insertions.
"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 insertion
Inserting at position 1 means adding the node at the start of the list.Step 2: Effect on the list head
The new node becomes the head, and its next points to the old 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 head's previous pointer
- Ignoring head pointer update
pos in a doubly linked list in Java?Solution
Step 1: Identify correct pointer updates
For insertion, newNode's next points to current's next, and prev points to current.Step 2: Update surrounding nodes
current.next's prev must point to newNode, and current's next must point to newNode.Final Answer:
newNode.next = current.next; newNode.prev = current; current.next.prev = newNode; current.next = newNode; -> Option DQuick Check:
Correct pointer updates = newNode.next = current.next; newNode.prev = current; current.next.prev = newNode; current.next = newNode; [OK]
- Swapping next and prev pointers
- Not updating current.next.prev
- Incorrect order of pointer assignments
Solution
Step 1: Locate position 3 in the list
Positions: 1=1, 2=2, 3=4 currently. Insert before node with value 4.Step 2: Insert new node with value 3 at position 3
New node's next points to node with value 4, prev points to node with value 2. Update pointers accordingly.Final Answer:
1 <-> 2 <-> 3 <-> 4 <-> null -> Option AQuick Check:
Insert at position 3 places 3 before 4 [OK]
- Inserting after position instead of at position
- Not updating prev pointer of next node
- Confusing node values and positions
Node newNode = new Node(5); Node current = head; current = current.next; newNode.next = current.next; newNode.prev = current; current.next = newNode; newNode.next.prev = newNode;What is the error in this code?
Solution
Step 1: Analyze newNode.next assignment
newNode.next = current.next; If current.next is null (inserting at end), newNode.next.prev causes error.Step 2: Identify NullPointerException risk
Accessing newNode.next.prev without checking null causes NullPointerException.Final Answer:
It may cause NullPointerException if current.next is null. -> Option BQuick Check:
Check for null before newNode.next.prev update [OK]
- Not checking for null before pointer updates
- Assuming list has more nodes without validation
- Forgetting to update head or tail pointers
Solution
Step 1: Insert 25 at position 3
Positions before insertion: 1=10, 2=20, 3=30, 4=40. Insert 25 before 30, so list becomes 10 <-> 20 <-> 25 <-> 30 <-> 40.Step 2: Delete node at position 4
After insertion, position 4 is node with value 30. Remove it by updating pointers: 25's next points to 40, 40's prev points to 25.Final Answer:
10 <-> 20 <-> 25 <-> 40 <-> null -> Option CQuick Check:
Insert then delete correct nodes = 10 <-> 20 <-> 25 <-> 40 <-> null [OK]
- Deleting wrong node after insertion
- Not updating pointers after deletion
- Confusing positions before and after insertion
Ready to prove it?
Pass the test and this pattern moves unknown
