Bird
Raised Fist0
DSA Rustprogramming~20 mins

Delete Node at Specific Position in DSA Rust - Practice Problems & Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Linked List Deletion Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
What is the output after deleting the node at position 2?
Consider a singly linked list with nodes containing values 10 -> 20 -> 30 -> 40 -> null. After deleting the node at position 2 (0-based index), what will be the printed list?
DSA Rust
struct Node {
    data: i32,
    next: Option<Box<Node>>,
}

fn delete_at_position(mut head: Option<Box<Node>>, pos: usize) -> Option<Box<Node>> {
    if pos == 0 {
        return head.and_then(|node| node.next);
    }
    let mut current = &mut head;
    for _ in 0..pos-1 {
        if let Some(ref mut node) = current {
            current = &mut node.next;
        } else {
            return head;
        }
    }
    if let Some(ref mut node) = current {
        if let Some(next_node) = node.next.take() {
            node.next = next_node.next;
        }
    }
    head
}

fn print_list(head: &Option<Box<Node>>) {
    let mut current = head;
    while let Some(node) = current {
        print!("{} -> ", node.data);
        current = &node.next;
    }
    println!("null");
}

fn main() {
    let node4 = Box::new(Node { data: 40, next: None });
    let node3 = Box::new(Node { data: 30, next: Some(node4) });
    let node2 = Box::new(Node { data: 20, next: Some(node3) });
    let node1 = Box::new(Node { data: 10, next: Some(node2) });
    let head = Some(node1);

    let new_head = delete_at_position(head, 2);
    print_list(&new_head);
}
A20 -> 30 -> 40 -> null
B10 -> 20 -> 40 -> null
C10 -> 30 -> 40 -> null
D10 -> 20 -> 30 -> null
Attempts:
2 left
💡 Hint
Remember that position 2 means the third node in the list (0-based). Deleting it removes the node with value 30.
❓ Predict Output
intermediate
2:00remaining
What is the output after deleting the head node?
Given a linked list 5 -> 15 -> 25 -> null, what will be the list after deleting the node at position 0?
DSA Rust
struct Node {
    data: i32,
    next: Option<Box<Node>>,
}

fn delete_at_position(mut head: Option<Box<Node>>, pos: usize) -> Option<Box<Node>> {
    if pos == 0 {
        return head.and_then(|node| node.next);
    }
    let mut current = &mut head;
    for _ in 0..pos-1 {
        if let Some(ref mut node) = current {
            current = &mut node.next;
        } else {
            return head;
        }
    }
    if let Some(ref mut node) = current {
        if let Some(next_node) = node.next.take() {
            node.next = next_node.next;
        }
    }
    head
}

fn print_list(head: &Option<Box<Node>>) {
    let mut current = head;
    while let Some(node) = current {
        print!("{} -> ", node.data);
        current = &node.next;
    }
    println!("null");
}

fn main() {
    let node3 = Box::new(Node { data: 25, next: None });
    let node2 = Box::new(Node { data: 15, next: Some(node3) });
    let node1 = Box::new(Node { data: 5, next: Some(node2) });
    let head = Some(node1);

    let new_head = delete_at_position(head, 0);
    print_list(&new_head);
}
A15 -> 25 -> null
B5 -> 25 -> null
C5 -> 15 -> null
Dnull
Attempts:
2 left
💡 Hint
Deleting position 0 removes the head node.
🔧 Debug
advanced
2:00remaining
What error occurs when deleting a node at an out-of-range position?
Given the linked list 1 -> 2 -> 3 -> null, what happens if we try to delete the node at position 5?
DSA Rust
struct Node {
    data: i32,
    next: Option<Box<Node>>,
}

fn delete_at_position(mut head: Option<Box<Node>>, pos: usize) -> Option<Box<Node>> {
    if pos == 0 {
        return head.and_then(|node| node.next);
    }
    let mut current = &mut head;
    for _ in 0..pos-1 {
        if let Some(ref mut node) = current {
            current = &mut node.next;
        } else {
            return head;
        }
    }
    if let Some(ref mut node) = current {
        if let Some(next_node) = node.next.take() {
            node.next = next_node.next;
        }
    }
    head
}

fn main() {
    let node3 = Box::new(Node { data: 3, next: None });
    let node2 = Box::new(Node { data: 2, next: Some(node3) });
    let node1 = Box::new(Node { data: 1, next: Some(node2) });
    let head = Some(node1);

    let new_head = delete_at_position(head, 5);
    // No print to focus on error
}
ACompilation error
BIndex out of bounds error
CNo error, list remains unchanged
DPanic due to None unwrap
Attempts:
2 left
💡 Hint
Check how the code handles positions beyond list length.
❓ Predict Output
advanced
2:00remaining
What is the output after deleting the last node?
Given the linked list 7 -> 14 -> 21 -> 28 -> null, what will be the list after deleting the node at position 3?
DSA Rust
struct Node {
    data: i32,
    next: Option<Box<Node>>,
}

fn delete_at_position(mut head: Option<Box<Node>>, pos: usize) -> Option<Box<Node>> {
    if pos == 0 {
        return head.and_then(|node| node.next);
    }
    let mut current = &mut head;
    for _ in 0..pos-1 {
        if let Some(ref mut node) = current {
            current = &mut node.next;
        } else {
            return head;
        }
    }
    if let Some(ref mut node) = current {
        if let Some(next_node) = node.next.take() {
            node.next = next_node.next;
        }
    }
    head
}

fn print_list(head: &Option<Box<Node>>) {
    let mut current = head;
    while let Some(node) = current {
        print!("{} -> ", node.data);
        current = &node.next;
    }
    println!("null");
}

fn main() {
    let node4 = Box::new(Node { data: 28, next: None });
    let node3 = Box::new(Node { data: 21, next: Some(node4) });
    let node2 = Box::new(Node { data: 14, next: Some(node3) });
    let node1 = Box::new(Node { data: 7, next: Some(node2) });
    let head = Some(node1);

    let new_head = delete_at_position(head, 3);
    print_list(&new_head);
}
A7 -> 21 -> 28 -> null
B7 -> 14 -> 28 -> null
C14 -> 21 -> 28 -> null
D7 -> 14 -> 21 -> null
Attempts:
2 left
💡 Hint
Deleting position 3 removes the last node.
🧠 Conceptual
expert
2:00remaining
What is the time complexity of deleting a node at a specific position in a singly linked list?
Consider a singly linked list with n nodes. What is the time complexity to delete a node at position k (0-based index)?
AO(k) because we need to traverse to the node before position k
BO(1) because we can directly access the node
CO(n) because we always traverse the entire list
DO(log n) because of binary search on the list
Attempts:
2 left
💡 Hint
Think about how you reach the node before the one to delete in a singly linked list.

Practice

(1/5)
1. What does deleting a node at a specific position in a linked list mean?
easy
A. Removing the node located at that position and linking its neighbors
B. Adding a new node at that position
C. Swapping two nodes in the list
D. Counting the number of nodes up to that position

Solution

  1. Step 1: Understand node deletion

    Deleting a node means removing it from the list and connecting the previous node to the next node.
  2. Step 2: Apply to specific position

    At a specific position, the node at that index is removed and neighbors are linked to maintain the list.
  3. Final Answer:

    Removing the node located at that position and linking its neighbors -> Option A
  4. Quick Check:

    Delete node = Remove and link neighbors [OK]
Hint: Delete means remove node and link neighbors [OK]
Common Mistakes:
  • Confusing deletion with insertion
  • Thinking deletion swaps nodes
  • Ignoring neighbor linking after deletion
2. Which Rust syntax correctly declares a mutable linked list node struct?
easy
A. struct Node { data: i32, mut next: Option }
B. struct Node { data: i32, next: Option }
C. struct Node { data: i32, next: Box<Node> }
D. struct Node { data: i32, next: &Node }

Solution

  1. Step 1: Check struct fields

    Rust linked list nodes use Option for next to allow null and ownership.
  2. Step 2: Validate mutability

    Mutability is controlled outside struct fields; 'mut' keyword inside struct is invalid.
  3. Final Answer:

    struct Node { data: i32, next: Option } -> Option B
  4. Quick Check:

    Option for next pointer [OK]
Hint: Use Option for next pointer [OK]
Common Mistakes:
  • Adding 'mut' inside struct fields
  • Using Box<Node> without Option causing no null
  • Using references (&Node) causing lifetime issues
3. What is the output linked list after deleting the node at position 2 (0-based) from this list: 10 -> 20 -> 30 -> 40 -> null?
fn main() {
    let mut head = Some(Box::new(Node { data: 10, next: Some(Box::new(Node { data: 20, next: Some(Box::new(Node { data: 30, next: Some(Box::new(Node { data: 40, next: None })) })) })) }));
    delete_at_position(&mut head, 2);
    print_list(&head);
}

fn delete_at_position(head: &mut Option, pos: usize) {
    if pos == 0 {
        *head = head.as_mut().and_then(|node| node.next.take());
        return;
    }
    let mut current = head.as_mut();
    for _ in 0..pos-1 {
        if let Some(node) = current {
            current = node.next.as_mut();
        }
    }
    if let Some(node) = current {
        node.next = node.next.as_mut().and_then(|next_node| next_node.next.take());
    }
}

fn print_list(head: &Option) {
    let mut current = head;
    while let Some(node) = current {
        print!("{} -> ", node.data);
        current = &node.next;
    }
    println!("null");
}

struct Node {
    data: i32,
    next: Option,
}
medium
A. 10 -> 20 -> 30 -> null
B. 10 -> 30 -> 40 -> null
C. 10 -> 20 -> 40 -> null
D. 20 -> 30 -> 40 -> null

Solution

  1. Step 1: Identify node to delete at position 2

    Position 2 node has data 30 in list 10 -> 20 -> 30 -> 40 -> null.
  2. Step 2: Remove node 30 and link 20 to 40

    After deletion, list becomes 10 -> 20 -> 40 -> null.
  3. Final Answer:

    10 -> 20 -> 40 -> null -> Option C
  4. Quick Check:

    Deleted node 30 at pos 2 [OK]
Hint: Delete node at pos links previous to next [OK]
Common Mistakes:
  • Deleting wrong node index
  • Not linking neighbors correctly
  • Confusing 0-based and 1-based positions
4. Identify the bug in this Rust function that deletes a node at a given position in a linked list:
fn delete_at_position(head: &mut Option, pos: usize) {
    let mut current = head.as_mut();
    for _ in 0..pos {
        if let Some(node) = current {
            current = node.next.as_mut();
        }
    }
    if let Some(node) = current {
        node.next = node.next.as_mut().and_then(|next_node| next_node.next.take());
    }
}
medium
A. All of the above
B. Loop runs one time too many, skipping the previous node
C. Does not update head pointer when deleting first node
D. Function does not handle deleting head node (pos 0)

Solution

  1. Step 1: Analyze loop range for traversal

    The loop runs from 0 to pos, moving current to node at pos, not previous node needed for deletion.
  2. Step 2: Check head deletion handling

    Function does not handle pos 0 case, so head pointer is never updated if first node is deleted.
  3. Step 3: Summarize all issues

    Loop skips previous node, no head update on pos 0, so all listed bugs exist.
  4. Final Answer:

    All of the above -> Option A
  5. Quick Check:

    Loop and head update bugs = All above [OK]
Hint: Check loop range and head update for deletion [OK]
Common Mistakes:
  • Looping to node instead of previous node
  • Ignoring deletion of head node
  • Not updating head pointer on deletion
5. Given a singly linked list with nodes: 5 -> 15 -> 25 -> 35 -> 45 -> null, write a Rust function to delete the node at position 3 (0-based). What will be the final list after deletion? Choose the correct final list output.
medium
A. 5 -> 15 -> 35 -> 45 -> null
B. 5 -> 15 -> 25 -> 35 -> null
C. 15 -> 25 -> 35 -> 45 -> null
D. 5 -> 15 -> 25 -> 45 -> null

Solution

  1. Step 1: Identify node at position 3

    Position 3 node has value 35 in list 5 -> 15 -> 25 -> 35 -> 45 -> null.
  2. Step 2: Delete node 35 and link 25 to 45

    After deletion, list becomes 5 -> 15 -> 25 -> 45 -> null.
  3. Final Answer:

    5 -> 15 -> 25 -> 45 -> null -> Option D
  4. Quick Check:

    Deleted node 35 at pos 3 [OK]
Hint: Delete node at pos and link neighbors [OK]
Common Mistakes:
  • Deleting wrong position node
  • Not linking previous node to next
  • Confusing 0-based indexing
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