Insert at Specific Position in Doubly Linked List in DSA Rust - Time & Space Complexity
We want to understand how the time to insert a new item at a specific place in a doubly linked list changes as the list grows.
How does the number of steps grow when the list gets longer?
Analyze the time complexity of the following code snippet.
struct Node {
data: i32,
prev: Option<*mut Node>,
next: Option>,
}
fn insert_at_position(head: &mut Option>, pos: usize, data: i32) {
let mut current = head.as_mut();
let mut index = 0;
while let Some(node) = current {
if index == pos - 1 {
// Insert logic here
break;
}
current = node.next.as_mut();
index += 1;
}
}
This code moves through the list to find the right spot, then inserts a new node there.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Traversing the list node by node to reach the position.
- How many times: Up to pos times, which can be as many as the list length n.
As the list gets longer, the steps to reach the position grow roughly in a straight line.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | Up to 10 steps |
| 100 | Up to 100 steps |
| 1000 | Up to 1000 steps |
Pattern observation: The number of steps grows directly with the list size.
Time Complexity: O(n)
This means the time to insert grows linearly with the list size because we must walk through nodes to find the spot.
[X] Wrong: "Insertion at any position in a doubly linked list is always fast and constant time."
[OK] Correct: You must first find the position by moving through nodes, which takes time proportional to the position, so it is not always instant.
Knowing how insertion time depends on list size helps you explain trade-offs in data structures clearly and shows you understand practical costs beyond just the code.
"What if we kept a pointer to the tail and wanted to insert near the end? How would the time complexity change?"
Practice
Solution
Step 1: Understand doubly linked list node connections
Each node has pointers to both previous and next nodes, so insertion requires updating these pointers around the insertion point.Step 2: Adjust pointers for insertion
To insert at a specific position, 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 nodes around the position -> Option AQuick Check:
Pointer updates = Adjust previous and next pointers [OK]
- Only updating one pointer and ignoring the other
- Not handling edge cases like inserting at head or tail
- Forgetting to update the new node's pointers
Solution
Step 1: Understand Rust ownership for doubly linked lists
Doubly linked lists require shared ownership and mutability, so Rc is used to allow multiple owners and interior mutability.Step 2: Identify correct pointer types
Option> allows optional pointers with shared ownership and mutability, suitable for prev and next pointers.Final Answer:
struct Node { data: i32, prev: Option>, next: Option> } -> Option AQuick Check:
Use Rc<RefCell> for shared mutable pointers [OK]
- Using Box without Rc causes ownership errors
- Using raw pointers without unsafe code
- Not wrapping pointers in Option for nullable links
Solution
Step 1: Identify insertion position and existing list
The list is 1 <-> 3, and we insert node 2 at position 2, meaning after node 1 and before node 3.Step 2: Insert node 2 between nodes 1 and 3
Update node 1's next to node 2, node 2's prev to node 1, node 2's next to node 3, and node 3's prev to node 2.Final Answer:
1 <-> 2 <-> 3 -> Option CQuick Check:
Insert at position 2 places node 2 between 1 and 3 [OK]
- Inserting node at wrong position
- Not updating both prev and next pointers
- Confusing position indexing starting at 0 or 1
Solution
Step 1: Analyze the error message and position
The panic 'index out of bounds' means the code tried to access a position beyond the list length. Position 3 requires at least 2 nodes before it.Step 2: Check list length against insertion position
If the list has fewer than 2 nodes, position 3 is invalid, causing the panic.Final Answer:
The list has fewer than 2 nodes, so position 3 is invalid -> Option BQuick Check:
Position > list length causes index out of bounds [OK]
- Not validating position against list length
- Assuming tail pointer updates fix index errors
- Ignoring null or empty list cases
Solution
Step 1: Locate position 3 in the list
Position 3 means the new node goes after the second node (20) and before the third node (30).Step 2: Update pointers to insert node 25
Set node 20's next to new node 25, new node 25's prev to node 20, new node 25's next to node 30, and node 30's prev to new node 25.Final Answer:
10 <-> 20 <-> 25 <-> 30 -> Option DQuick Check:
Insert at position 3 places 25 between 20 and 30 [OK]
- Inserting node at wrong position
- Not updating both prev and next pointers correctly
- Confusing position indexing starting at 0 or 1
Ready to prove it?
Pass the test and this pattern moves unknown
