Delete Node at End in DSA Rust - Time & Space Complexity
We want to understand how long it takes to remove the last item from a linked list.
How does the time needed change as the list gets bigger?
Analyze the time complexity of the following code snippet.
fn delete_at_end(head: &mut Option>) {
if head.is_none() {
return;
}
if head.as_ref().unwrap().next.is_none() {
*head = None;
return;
}
let mut current = head.as_mut().unwrap();
while current.next.as_ref().unwrap().next.is_some() {
current = current.next.as_mut().unwrap();
}
current.next = None;
}
This code removes the last node from a singly linked list by walking through the list to find the second last node.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: The while loop that moves through nodes one by one.
- How many times: It runs once for each node until the second last node is found.
As the list grows, the code checks more nodes one by one until it reaches the end.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 9 steps to reach second last node |
| 100 | About 99 steps |
| 1000 | About 999 steps |
Pattern observation: The number of steps grows roughly the same as the list size.
Time Complexity: O(n)
This means the time to delete the last node grows linearly with the list size.
[X] Wrong: "Deleting the last node takes constant time because we just remove it."
[OK] Correct: We must first find the node before the last one, which takes time proportional to the list length.
Understanding this helps you explain how linked lists work and why some operations take longer than others.
"What if the list had a pointer to the last node? How would the time complexity change?"
Practice
Delete Node at End do in a linked list?Solution
Step 1: Understand the linked list structure
A linked list is a chain of nodes where each node points to the next one.Step 2: Identify the effect of deleting the last node
Deleting the last node means removing the node that has no next node, effectively shortening the list by one node at the end.Final Answer:
Removes the last node from the linked list -> Option BQuick Check:
Delete Node at End = Remove last node [OK]
- Confusing delete at end with delete at start
- Thinking it adds a node instead of removing
- Assuming it searches without deleting
Solution
Step 1: Analyze the code logic for deleting last node
fn delete_end(head: &mut Option) { if head.is_none() { return; } let mut current = head.as_mut().unwrap(); while current.next.as_ref().unwrap().next.is_some() { current = current.next.as_mut().unwrap(); } current.next = None; } traverses the list until the second last node and sets its next to None, removing the last node.Step 2: Check other options for correctness
fn delete_end(head: &mut Option) { *head = None; } deletes the entire list, not just last node. fn delete_end(head: &mut Option) { let mut current = head.as_mut().unwrap(); current.next = None; } incorrectly sets next of head only. fn delete_end(head: &mut Option) { while head.is_some() { head = head.unwrap().next; } } tries to reassign head incorrectly causing error.Final Answer:
Option A code correctly deletes the last node -> Option AQuick Check:
Traverse to second last node and set next to None [OK]
- Setting head to None deletes whole list
- Not checking for empty or single node list
- Incorrectly reassigning head pointer
struct Node { val: i32, next: Option }
fn print_list(head: &Option) {
let mut current = head;
while let Some(node) = current {
print!("{} -> ", node.val);
current = &node.next;
}
println!("null");
}
fn main() {
let mut head = Some(Box::new(Node { val: 1, next: Some(Box::new(Node { val: 2, next: Some(Box::new(Node { val: 3, next: None })) })) }));
delete_end(&mut head);
print_list(&head);
}
fn delete_end(head: &mut Option) {
if head.is_none() { return; }
if head.as_ref().unwrap().next.is_none() { *head = None; return; }
let mut current = head.as_mut().unwrap();
while current.next.as_ref().unwrap().next.is_some() {
current = current.next.as_mut().unwrap();
}
current.next = None;
}Solution
Step 1: Understand initial list and delete_end effect
The list starts as 1 -> 2 -> 3 -> null. delete_end removes last node (3).Step 2: Trace print_list after deletion
After deletion, list is 1 -> 2 -> null, so print_list outputs "1 -> 2 -> null".Final Answer:
1 -> 2 -> null -> Option CQuick Check:
Last node removed, list ends at 2 [OK]
- Expecting original list unchanged
- Confusing print output format
- Not handling single node list case
fn delete_end(head: &mut Option) {
let mut current = head.as_mut().unwrap();
while current.next.is_some() {
current = current.next.as_mut().unwrap();
}
current.next = None;
}Solution
Step 1: Locate the unwrap without check
let mut current = head.as_mut().unwrap(); executes without prior if head.is_none() { return; }, causing panic on empty list when as_mut() returns None and unwrap panics.Step 2: Verify other options
The code does not modify head directly. The while loop terminates when current.next.is_none(). Setting current.next = None after reaching the last node causes no panic.Final Answer:
It panics because it tries to unwrap None when list is empty -> Option DQuick Check:
head.as_mut().unwrap() without empty check [OK]
- No check for empty list before unwrap
- Not checking for empty or single node list
- Looping until current.next is None instead of second last
Solution
Step 1: Check for empty and single node list
fn delete_end(head: &mut Option) { if head.is_none() { return; } if head.as_ref().unwrap().next.is_none() { *head = None; return; } let mut current = head.as_mut().unwrap(); while current.next.as_ref().unwrap().next.is_some() { current = current.next.as_mut().unwrap(); } current.next = None; } correctly checks if list is empty or has only one node, setting head to None in that case.Step 2: Traverse to second last node and delete last
fn delete_end(head: &mut Option) { if head.is_none() { return; } if head.as_ref().unwrap().next.is_none() { *head = None; return; } let mut current = head.as_mut().unwrap(); while current.next.as_ref().unwrap().next.is_some() { current = current.next.as_mut().unwrap(); } current.next = None; } then traverses to second last node and sets its next to None, deleting last node properly.Final Answer:
Option A correctly handles all cases including single node list -> Option AQuick Check:
Check empty/single node, then delete last node [OK]
- Not handling single node list causing panic
- Setting current = None instead of current.next = None
- Deleting entire list instead of last node
Ready to prove it?
Pass the test and this pattern moves unknown
