Bird
Raised Fist0
DSA Rustprogramming~10 mins

Delete Node at End in DSA Rust - Execution Trace

Choose your learning style10 modes available
Concept Flow - Delete Node at End
Start at head
↓
Is list empty?
Yes→Stop, nothing to delete
No↓
Is only one node?
Yes→Delete head, set head to None
No↓
Traverse to second last node
↓
Set second last node's next to None
↓
Delete last node
↓
End
Start from the head, check if list is empty or has one node, otherwise traverse to second last node and remove the last node by updating pointers.
Execution Sample
DSA Rust
fn delete_at_end(head: &mut Option<Box<Node>>) {
    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;
}
Deletes the last node from a singly linked list by traversing to the second last node and removing the last.
Execution Table
StepActionCurrent Node ValueNext Node ValueList State
1Check if head is NoneN/AN/AList: 1 -> 2 -> 3 -> null
2Check if only one node12List: 1 -> 2 -> 3 -> null
3Set current to head12List: 1 -> 2 -> 3 -> null
4Traverse to second last node23List: 1 -> 2 -> 3 -> null
5Set current.next to None (delete last node)2NoneList: 1 -> 2 -> null
6End2NoneList after deletion: 1 -> 2 -> null
💡 Last node deleted by setting second last node's next to None
Variable Tracker
VariableStartAfter Step 3After Step 4After Step 5Final
head1 -> 2 -> 3 -> null1 -> 2 -> 3 -> null1 -> 2 -> 3 -> null1 -> 2 -> null1 -> 2 -> null
currentNoneNode(1)Node(2)Node(2)Node(2)
Key Moments - 2 Insights
Why do we check if the list has only one node separately?
Because if there is only one node, deleting it means setting head to None. This check is shown in execution_table row 2.
Why do we stop traversal at the second last node, not the last?
We need to update the second last node's next pointer to None to remove the last node. The traversal stops at the second last node as shown in execution_table row 4.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the value of 'current' after step 4?
ANode with value 2
BNode with value 3
CNode with value 1
DNone
💡 Hint
Check the 'Current Node Value' column at step 4 in execution_table
At which step does the last node get deleted?
AStep 3
BStep 4
CStep 5
DStep 6
💡 Hint
Look at the 'Action' column describing deletion in execution_table
If the list had only one node, what would happen at step 2?
AThe function would continue traversing
BThe function would return after setting head to None
CThe function would crash
DThe function would delete the second node
💡 Hint
Refer to key_moments explanation about single node deletion and execution_table row 2
Concept Snapshot
Delete Node at End in Singly Linked List:
- Check if list is empty: do nothing
- If only one node: set head to None
- Else traverse to second last node
- Set second last node's next to None
- Last node is removed
- Time complexity: O(n)
Full Transcript
To delete the last node in a singly linked list, first check if the list is empty. If it is, do nothing. If the list has only one node, delete it by setting the head to None. Otherwise, start from the head and move through the list until you reach the second last node. Then, set its next pointer to None, effectively removing the last node. This process takes linear time because you may need to traverse the entire list.

Practice

(1/5)
1. What does the operation Delete Node at End do in a linked list?
easy
A. Adds a new node at the end of the linked list
B. Removes the last node from the linked list
C. Removes the first node from the linked list
D. Searches for a node at the end of the linked list

Solution

  1. Step 1: Understand the linked list structure

    A linked list is a chain of nodes where each node points to the next one.
  2. 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.
  3. Final Answer:

    Removes the last node from the linked list -> Option B
  4. Quick Check:

    Delete Node at End = Remove last node [OK]
Hint: Deleting at end always removes the last node [OK]
Common Mistakes:
  • Confusing delete at end with delete at start
  • Thinking it adds a node instead of removing
  • Assuming it searches without deleting
2. Which of the following Rust code snippets correctly deletes the last node of a singly linked list?
easy
A. 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; }
B. fn delete_end(head: &mut Option) { *head = None; }
C. fn delete_end(head: &mut Option) { let mut current = head.as_mut().unwrap(); current.next = None; }
D. fn delete_end(head: &mut Option) { while head.is_some() { head = head.unwrap().next; } }

Solution

  1. 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.
  2. 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.
  3. Final Answer:

    Option A code correctly deletes the last node -> Option A
  4. Quick Check:

    Traverse to second last node and set next to None [OK]
Hint: Traverse to second last node, then set next to None [OK]
Common Mistakes:
  • Setting head to None deletes whole list
  • Not checking for empty or single node list
  • Incorrectly reassigning head pointer
3. Given the Rust linked list code below, what will be the printed output after deleting the last node?
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;
}
medium
A. null
B. 1 -> 2 -> 3 -> null
C. 1 -> 2 -> null
D. 1 -> null

Solution

  1. Step 1: Understand initial list and delete_end effect

    The list starts as 1 -> 2 -> 3 -> null. delete_end removes last node (3).
  2. Step 2: Trace print_list after deletion

    After deletion, list is 1 -> 2 -> null, so print_list outputs "1 -> 2 -> null".
  3. Final Answer:

    1 -> 2 -> null -> Option C
  4. Quick Check:

    Last node removed, list ends at 2 [OK]
Hint: Deleting last node removes final value from print output [OK]
Common Mistakes:
  • Expecting original list unchanged
  • Confusing print output format
  • Not handling single node list case
4. The following Rust function to delete the last node of a linked list causes a runtime panic. What is the error?
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;
}
medium
A. It panics because it sets current to None instead of current.next
B. It panics because it modifies head directly without checking
C. It panics because it never stops the loop
D. It panics because it tries to unwrap None when list is empty

Solution

  1. 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.
  2. 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.
  3. Final Answer:

    It panics because it tries to unwrap None when list is empty -> Option D
  4. Quick Check:

    head.as_mut().unwrap() without empty check [OK]
Hint: Check head.is_none() before any unwrap on head [OK]
Common Mistakes:
  • 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
5. You have a singly linked list with nodes containing values [10, 20, 30, 40]. Write a Rust function to delete the last node and return the updated list head. Which of the following implementations correctly handles the edge case when the list has only one node?
hard
A. 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; }
B. 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 = None; }
C. fn delete_end(head: &mut Option) { if head.is_none() { return; } let mut current = head.as_mut().unwrap(); while current.next.is_some() { current = current.next.as_mut().unwrap(); } current.next = None; }
D. fn delete_end(head: &mut Option) { *head = None; }

Solution

  1. 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.
  2. 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.
  3. Final Answer:

    Option A correctly handles all cases including single node list -> Option A
  4. Quick Check:

    Check empty/single node, then delete last node [OK]
Hint: Always check if list has one node before deleting last [OK]
Common Mistakes:
  • Not handling single node list causing panic
  • Setting current = None instead of current.next = None
  • Deleting entire list instead of last node
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard