Bird
Raised Fist0
DSA Rustprogramming~20 mins

Remove Nth Node from End of List in DSA Rust - Practice Problems & Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Remove Nth Node Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output of removing 2nd node from end in a 5-node list
What is the printed linked list after removing the 2nd node from the end in the list 1 -> 2 -> 3 -> 4 -> 5 -> null?
DSA Rust
struct ListNode {
    val: i32,
    next: Option<Box<ListNode>>,
}

fn remove_nth_from_end(mut head: Option<Box<ListNode>>, n: i32) -> Option<Box<ListNode>> {
    let mut dummy = Box::new(ListNode { val: 0, next: head });
    let mut first = &dummy;
    let mut second = &mut dummy;

    for _ in 0..=n {
        first = first.next.as_ref().unwrap();
    }

    while let Some(node) = first.next.as_ref() {
        first = node;
        second = second.next.as_mut().unwrap();
    }

    let next = second.next.as_mut().unwrap().next.take();
    second.next = next;

    dummy.next
}

fn print_list(head: &Option<Box<ListNode>>) {
    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(ListNode { val: 1, next: Some(Box::new(ListNode { val: 2, next: Some(Box::new(ListNode { val: 3, next: Some(Box::new(ListNode { val: 4, next: Some(Box::new(ListNode { val: 5, next: None })) })) })) })) }));
    head = remove_nth_from_end(head, 2);
    print_list(&head);
}
A1 -> 2 -> 4 -> 5 -> null
B1 -> 3 -> 4 -> 5 -> null
C1 -> 2 -> 3 -> 5 -> null
D2 -> 3 -> 4 -> 5 -> null
Attempts:
2 left
💡 Hint
Remember the 2nd node from the end is the 4th node from the start in a 5-node list.
❓ Predict Output
intermediate
2:00remaining
Output after removing last node from a 3-node list
What is the printed linked list after removing the 1st node from the end in the list 10 -> 20 -> 30 -> null?
DSA Rust
struct ListNode {
    val: i32,
    next: Option<Box<ListNode>>,
}

fn remove_nth_from_end(mut head: Option<Box<ListNode>>, n: i32) -> Option<Box<ListNode>> {
    let mut dummy = Box::new(ListNode { val: 0, next: head });
    let mut first = &dummy;
    let mut second = &mut dummy;

    for _ in 0..=n {
        first = first.next.as_ref().unwrap();
    }

    while let Some(node) = first.next.as_ref() {
        first = node;
        second = second.next.as_mut().unwrap();
    }

    let next = second.next.as_mut().unwrap().next.take();
    second.next = next;

    dummy.next
}

fn print_list(head: &Option<Box<ListNode>>) {
    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(ListNode { val: 10, next: Some(Box::new(ListNode { val: 20, next: Some(Box::new(ListNode { val: 30, next: None })) })) }));
    head = remove_nth_from_end(head, 1);
    print_list(&head);
}
A10 -> 20 -> 30 -> null
B10 -> 30 -> null
C20 -> 30 -> null
D10 -> 20 -> null
Attempts:
2 left
💡 Hint
Removing the 1st node from the end means removing the last node.
🔧 Debug
advanced
2:00remaining
Identify the error in this Rust code for removing nth node from end
What error does this Rust code produce when trying to remove the 3rd node from the end in a 3-node list?
DSA Rust
struct ListNode {
    val: i32,
    next: Option<Box<ListNode>>,
}

fn remove_nth_from_end(mut head: Option<Box<ListNode>>, n: i32) -> Option<Box<ListNode>> {
    let mut dummy = Box::new(ListNode { val: 0, next: head });
    let mut first = &dummy;
    let mut second = &mut dummy;

    for _ in 0..=n {
        first = first.next.as_ref().unwrap();
    }

    while let Some(node) = first.next.as_ref() {
        first = node;
        second = second.next.as_mut().unwrap();
    }

    let next = second.next.as_mut().unwrap().next.take();
    second.next = next;

    dummy.next
}

fn main() {
    let mut head = Some(Box::new(ListNode { val: 1, next: Some(Box::new(ListNode { val: 2, next: Some(Box::new(ListNode { val: 3, next: None })) })) }));
    head = remove_nth_from_end(head, 3);
}
Apanic at unwrap on None value
Bcorrect output: 2 -> 3 -> null
Cinfinite loop
Dcompilation error: mismatched types
Attempts:
2 left
💡 Hint
Check the for loop range and how many times unwrap is called.
🧠 Conceptual
advanced
1:00remaining
Why use a dummy node in removing nth node from end?
Why is a dummy node used at the start of the linked list when removing the nth node from the end?
ATo simplify edge cases like removing the head node
BTo reduce memory usage by reusing nodes
CTo speed up the traversal by skipping nodes
DTo avoid using recursion in the solution
Attempts:
2 left
💡 Hint
Think about what happens if the node to remove is the first node.
🚀 Application
expert
3:00remaining
Resulting list after removing 4th node from end in 6-node list
Given the list 5 -> 10 -> 15 -> 20 -> 25 -> 30 -> null, what is the list after removing the 4th node from the end?
DSA Rust
struct ListNode {
    val: i32,
    next: Option<Box<ListNode>>,
}

fn remove_nth_from_end(mut head: Option<Box<ListNode>>, n: i32) -> Option<Box<ListNode>> {
    let mut dummy = Box::new(ListNode { val: 0, next: head });
    let mut first = &dummy;
    let mut second = &mut dummy;

    for _ in 0..=n {
        first = first.next.as_ref().unwrap();
    }

    while let Some(node) = first.next.as_ref() {
        first = node;
        second = second.next.as_mut().unwrap();
    }

    let next = second.next.as_mut().unwrap().next.take();
    second.next = next;

    dummy.next
}

fn print_list(head: &Option<Box<ListNode>>) {
    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(ListNode { val: 5, next: Some(Box::new(ListNode { val: 10, next: Some(Box::new(ListNode { val: 15, next: Some(Box::new(ListNode { val: 20, next: Some(Box::new(ListNode { val: 25, next: Some(Box::new(ListNode { val: 30, next: None })) })) })) })) })) }));
    head = remove_nth_from_end(head, 4);
    print_list(&head);
}
A5 -> 10 -> 15 -> 25 -> 30 -> null
B5 -> 10 -> 20 -> 25 -> 30 -> null
C5 -> 15 -> 20 -> 25 -> 30 -> null
D10 -> 15 -> 20 -> 25 -> 30 -> null
Attempts:
2 left
💡 Hint
The 4th node from the end is the 3rd node from the start in a 6-node list.

Practice

(1/5)
1. What does the problem Remove Nth Node from End of List ask you to do?
easy
A. Reverse the linked list up to the nth node
B. Add a new node at the nth position from the start
C. Delete the node that is the nth from the end in a linked list
D. Find the value of the nth node from the start

Solution

  1. Step 1: Understand the problem statement

    The problem requires removing the node that is nth from the end in a singly linked list.
  2. Step 2: Clarify the operation

    We do not add or reverse nodes; we specifically delete the nth node counting from the list's end.
  3. Final Answer:

    Delete the node that is the nth from the end in a linked list -> Option C
  4. Quick Check:

    Remove nth from end = Delete nth from end [OK]
Hint: Focus on 'remove nth from end' means delete that node [OK]
Common Mistakes:
  • Confusing nth from end with nth from start
  • Thinking the problem is about reversing the list
  • Assuming insertion instead of deletion
2. Which Rust code snippet correctly declares a singly linked list node struct for this problem?
easy
A. struct ListNode { val: i32, next: &ListNode }
B. struct ListNode { val: i32, next: Option }
C. struct ListNode { val: i32, next: Box<ListNode> }
D. struct ListNode { val: i32, next: Option<&ListNode> }

Solution

  1. Step 1: Recall Rust ownership for linked lists

    Rust uses Option to allow nullable next pointers with ownership.
  2. Step 2: Check each option

    struct ListNode { val: i32, next: Option } uses Option which is correct. Options A and D use references without ownership, which is unsafe. struct ListNode { val: i32, next: Box<ListNode> } lacks Option, so no null end.
  3. Final Answer:

    struct ListNode { val: i32, next: Option } -> Option B
  4. Quick Check:

    Rust linked list node = Option [OK]
Hint: Use Option for nullable owned pointers in Rust [OK]
Common Mistakes:
  • Using references (&) instead of Box for next
  • Omitting Option causing no null end
  • Forgetting ownership rules in Rust
3. Given the Rust code below, what is the linked list output after removing the 2nd node from the end?
let mut list = Some(Box::new(ListNode { val: 1, next: Some(Box::new(ListNode { val: 2, next: Some(Box::new(ListNode { val: 3, next: None })) })) }));
// Remove 2nd from end
let result = remove_nth_from_end(list, 2);
// Print list values
medium
A. 1 -> 2 -> 3 -> null
B. 1 -> 2 -> null
C. 2 -> 3 -> null
D. 1 -> 3 -> null

Solution

  1. Step 1: Identify the list and node to remove

    The list is 1 -> 2 -> 3. Removing 2nd from end means removing node with value 2.
  2. Step 2: Remove the node and update links

    After removal, list becomes 1 -> 3 -> null.
  3. Final Answer:

    1 -> 3 -> null -> Option D
  4. Quick Check:

    Remove 2nd from end removes '2' [OK]
Hint: Count from end, remove that node, link neighbors [OK]
Common Mistakes:
  • Removing the wrong node counting from start
  • Not updating next pointers correctly
  • Confusing 2nd from end with 2nd from start
4. The following Rust function aims to remove the nth node from the end but has a bug. What is the bug?
fn remove_nth_from_end(head: Option, n: i32) -> Option {
    let mut dummy = Box::new(ListNode { val: 0, next: head });
    let mut first = &dummy;
    let mut second = &mut dummy;
    for _ in 0..n {
        first = first.next.as_ref().unwrap();
    }
    while first.next.is_some() {
        first = first.next.as_ref().unwrap();
        second = second.next.as_mut().unwrap();
    }
    second.next = second.next.as_mut().unwrap().next.take();
    dummy.next
}
medium
A. Using unwrap() without checking may panic if list is short
B. second is immutable, cannot modify next pointer
C. first is an immutable reference, cannot advance correctly
D. dummy.next is returned before modification

Solution

  1. Step 1: Analyze unwrap usage

    The code uses unwrap() on next without checking if next exists, which can panic if n is larger than list length.
  2. Step 2: Confirm other references are valid

    first is immutable reference but used only for reading, second is mutable for modification, dummy.next returned after modification.
  3. Final Answer:

    Using unwrap() without checking may panic if list is short -> Option A
  4. Quick Check:

    unwrap() without safety check causes panic [OK]
Hint: Check unwrap() calls for possible panics [OK]
Common Mistakes:
  • Confusing mutable and immutable references
  • Ignoring unwrap panic risks
  • Assuming dummy.next is returned too early
5. You want to remove the nth node from the end of a linked list in Rust. Which approach correctly handles the edge case when the node to remove is the head node?
hard
A. Use a dummy node before head and adjust dummy.next after removal
B. Directly remove head without dummy node by shifting head pointer
C. Traverse twice: first to count length, second to remove node without dummy
D. Convert list to vector, remove element, then rebuild list

Solution

  1. Step 1: Understand the edge case

    If the node to remove is the head, removing it directly is tricky because head pointer must change.
  2. Step 2: Use dummy node technique

    Adding a dummy node before head simplifies removal by always having a previous node to update, including when head is removed.
  3. Final Answer:

    Use a dummy node before head and adjust dummy.next after removal -> Option A
  4. Quick Check:

    Dummy node handles head removal edge case [OK]
Hint: Add dummy node to simplify head removal [OK]
Common Mistakes:
  • Not handling head removal separately
  • Forgetting to update head pointer
  • Using inefficient vector conversion
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