Delete Node by Value in DSA Rust - Time & Space Complexity
We want to understand how long it takes to remove a node by its value 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_node(head: &mut Option>, value: i32) {
let mut current = head;
while let Some(ref mut node) = current {
if node.data == value {
*current = node.next.take();
return;
}
current = &mut node.next;
}
}
This code searches the linked list for the first node with the given value and removes it.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Loop through nodes one by one.
- How many times: Up to the number of nodes until the value is found or list ends.
As the list grows, the time to find the node grows roughly in a straight line.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | Up to 10 checks |
| 100 | Up to 100 checks |
| 1000 | Up to 1000 checks |
Pattern observation: The number of steps grows directly with the list size.
Time Complexity: O(n)
This means the time to delete a node grows linearly with the number of nodes in the list.
[X] Wrong: "Deleting a node by value always takes constant time because we just remove it."
[OK] Correct: We must first find the node by checking each one, which can take time proportional to the list size.
Understanding this helps you explain how searching and modifying linked lists work in real code.
"What if the list was doubly linked? How would the time complexity change?"
Practice
Solution
Step 1: Understand the operation
Deleting a node by value means finding the first node that matches the value and removing it from the list.Step 2: Compare options
Only Removes the first node that contains the given value describes removing the first node with the given value, which matches the operation.Final Answer:
Removes the first node that contains the given value -> Option AQuick Check:
Delete node by value = Remove first matching node [OK]
- Confusing deletion by value with adding nodes
- Thinking it deletes all nodes with the value
- Mixing deletion by value with reversing the list
Solution
Step 1: Analyze pointer usage in Rust linked list deletion
Rust linked lists use mutable references to update links. let mut current = &mut head; while let Some(ref mut node) = current { if node.val == val { *current = node.next.take(); break; } current = &mut node.next; } uses `&mut` references and `take()` to remove the node correctly.Step 2: Check other options for correctness
let mut current = head; while current.is_some() { if current.val == val { current = current.next; break; } current = current.next; } uses immutable references and does not update links properly. for node in head.iter() { if node.val == val { head.remove(node); break; } } uses a for loop which is invalid for linked lists. while let Some(node) = head { if node.val == val { head = node.next; } } does not handle mutable references correctly.Final Answer:
Uses `&mut` references and `take()` to remove the node correctly -> Option AQuick Check:
Use mutable references and take() to delete node [OK]
- Using immutable references to modify list
- Trying to iterate with for loop on linked list
- Not updating the previous node's next pointer
struct Node {
val: i32,
next: Option,
}
let mut head = Some(Box::new(Node { val: 1, next: Some(Box::new(Node { val: 3, next: Some(Box::new(Node { val: 5, next: None })) })) }));
let val = 3;
let mut current = &mut head;
while let Some(ref mut node) = current {
if node.val == val {
*current = node.next.take();
break;
}
current = &mut node.next;
}
Solution
Step 1: Understand the initial list structure
The list is 1 -> 3 -> 5 -> null.Step 2: Trace deletion of node with value 3
The loop finds node with val=3 and removes it by linking previous node's next to node 3's next (which is node 5).Final Answer:
1 -> 5 -> null -> Option CQuick Check:
Deleted node 3, list is 1 -> 5 [OK]
- Not updating previous node's next pointer
- Deleting wrong node due to incorrect comparison
- Confusing node values and positions
let mut current = &head;
while let Some(ref node) = current {
if node.val == val {
current = &node.next;
break;
}
current = &node.next;
}Solution
Step 1: Check reference mutability
The code uses `&head` which is immutable, so it cannot modify the list links.Step 2: Understand why mutable reference is needed
To delete a node, we must change pointers, requiring `&mut` references, which are missing here.Final Answer:
Using immutable reference `&head` prevents modifying the list -> Option BQuick Check:
Modify list needs mutable references [OK]
- Ignoring mutability requirements in Rust
- Assuming immutable references can change list
- Overlooking compiler error messages
Solution
Step 1: Understand requirement to delete all nodes with value 4
We must remove every node containing 4, not just the first.Step 2: Use mutable references to update links repeatedly
Loop through list with mutable pointer, deleting nodes with value 4 by updating links until none remain.Step 3: Reject other options
Delete only the first node with value 4 and stop deletes only first node. Traverse list with immutable references and skip nodes with value 4 without modifying links does not modify list. Replace all node values 4 with 0 instead of deleting nodes changes values but does not delete nodes.Final Answer:
Use a loop with mutable reference to current node pointer, repeatedly deleting nodes with value 4 until none remain -> Option DQuick Check:
Delete all matching nodes by looping with mutable refs [OK]
- Deleting only first matching node
- Using immutable references to try deleting
- Confusing value replacement with deletion
Ready to prove it?
Pass the test and this pattern moves unknown
