Insert at End of Doubly Linked List in DSA Rust - Time & Space Complexity
We want to understand how the time to insert a new item at the end of a doubly linked list changes as the list grows.
How does the number of steps needed 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_end(head: &mut Option>, value: i32) {
let mut new_node = Box::new(Node { data: value, prev: None, next: None });
if head.is_none() {
*head = Some(new_node);
return;
}
let mut current = head.as_mut().unwrap();
while current.next.is_some() {
current = current.next.as_mut().unwrap();
}
new_node.prev = Some(&mut **current as *mut Node);
current.next = Some(new_node);
}
This code adds a new node with the given value at the end of a doubly linked list by traversing from the head to the last node.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: The while loop that moves through each node from the head to the end.
- How many times: It runs once for each node until the last one, so roughly as many times as the list length.
As the list gets longer, the number of steps to reach the end grows directly with the number of nodes.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 10 steps to reach the end |
| 100 | About 100 steps to reach the end |
| 1000 | About 1000 steps to reach the end |
Pattern observation: The steps increase linearly as the list size increases.
Time Complexity: O(n)
This means the time to insert at the end grows in direct proportion to the list size.
[X] Wrong: "Inserting at the end is always fast and takes constant time."
[OK] Correct: Without a pointer to the last node, you must walk through the whole list, so time grows with list length.
Understanding how list traversal affects insertion time helps you explain trade-offs in data structure design clearly and confidently.
"What if we kept a pointer to the last node? How would the time complexity change?"
Practice
Solution
Step 1: Understand the structure of a doubly linked list
A doubly linked list has nodes connected forward and backward, allowing traversal in both directions.Step 2: Purpose of insertion at the end
Inserting at the end means adding a new node after the current last node, updating pointers accordingly.Final Answer:
To add a new element after the last node -> Option CQuick Check:
Insert at end = add after last node [OK]
- Confusing insertion at end with deletion
- Thinking insertion reverses the list
- Assuming insertion happens at the start
Solution
Step 1: Create new node with given value
Use Node::new(value) to create a new node.Step 2: Link new node after tail
Set tail.next to new_node and new_node.prev to tail to insert at end.Final Answer:
let new_node = Node::new(value); tail.next = Some(new_node); new_node.prev = Some(tail); -> Option DQuick Check:
New node after tail = let new_node = Node::new(value); tail.next = Some(new_node); new_node.prev = Some(tail); [OK]
- Linking new node before head instead of after tail
- Swapping next and prev pointers incorrectly
- Not updating tail's next pointer
struct Node {
value: i32,
prev: Option,
next: Option,
}
// Assume list initially: 1 <-> 2 <-> 3
// Insert 4 at end
// Print list from head to tail
Solution
Step 1: Understand initial list structure
The list is 1 linked to 2, linked to 3, with 3 as tail.Step 2: Insert 4 at the end
Adding 4 after 3 updates 3.next to 4 and 4.prev to 3.Final Answer:
1 <-> 2 <-> 3 <-> 4 <-> null -> Option AQuick Check:
Insert at end adds 4 after 3 [OK]
- Inserting new node at start instead of end
- Not updating next pointer of last node
- Printing list without new node
fn insert_end(tail: &mut Option, value: i32) {
let mut new_node = Box::new(Node::new(value));
new_node.prev = tail.clone();
tail = Some(new_node);
}Solution
Step 1: Check pointer updates
tail is reassigned locally, but tail's next pointer is not updated to link new_node.Step 2: Correct linking needed
Must set tail's next to new_node and new_node.prev to tail, then update tail reference.Final Answer:
tail is not updated correctly; should update tail's next pointer -> Option AQuick Check:
Update tail.next to new_node [OK]
- Only reassigning tail without linking nodes
- Setting prev pointer incorrectly
- Ignoring ownership rules in Rust
Solution
Step 1: Understand list before insertion
List is 10 <-> 20 <-> 30; middle is 20 (second of three nodes).Step 2: Insert 25 at end and find new middle
List becomes 10 <-> 20 <-> 30 <-> 25. With 4 nodes, middle is between 20 and 30, usually the second node (20) to keep balance.Final Answer:
20 -> Option BQuick Check:
Middle of 4 nodes = 2nd node [OK]
- Moving middle pointer to last inserted node
- Choosing last node as middle
- Not updating middle pointer after insertion
Ready to prove it?
Pass the test and this pattern moves unknown
