Bird
Raised Fist0
DSA Rustprogramming~20 mins

Push Using Linked List Node in DSA Rust - Practice Problems & Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Linked List Push 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 pushing nodes in a linked list?
Consider a singly linked list where we push nodes at the front. What will be the printed linked list after the following push operations?
DSA Rust
struct Node {
    data: i32,
    next: Option<Box<Node>>,
}

struct LinkedList {
    head: Option<Box<Node>>,
}

impl LinkedList {
    fn new() -> Self {
        LinkedList { head: None }
    }

    fn push(&mut self, data: i32) {
        let new_node = Box::new(Node {
            data,
            next: self.head.take(),
        });
        self.head = Some(new_node);
    }

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

fn main() {
    let mut list = LinkedList::new();
    list.push(10);
    list.push(20);
    list.push(30);
    list.print_list();
}
A10 -> 20 -> 30 -> null
Bnull -> 30 -> 20 -> 10
C30 -> 20 -> 10 -> null
D20 -> 30 -> 10 -> null
Attempts:
2 left
💡 Hint
Remember that pushing adds a new node at the front of the list.
❓ Predict Output
intermediate
2:00remaining
What is the linked list after pushing nodes with these values?
Given the push method that inserts nodes at the front, what is the linked list after pushing 5, then 15, then 25?
DSA Rust
struct Node {
    data: i32,
    next: Option<Box<Node>>,
}

struct LinkedList {
    head: Option<Box<Node>>,
}

impl LinkedList {
    fn new() -> Self {
        LinkedList { head: None }
    }

    fn push(&mut self, data: i32) {
        let new_node = Box::new(Node {
            data,
            next: self.head.take(),
        });
        self.head = Some(new_node);
    }

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

fn main() {
    let mut list = LinkedList::new();
    list.push(5);
    list.push(15);
    list.push(25);
    list.print_list();
}
A25 -> 15 -> 5 -> null
B5 -> 15 -> 25 -> null
C15 -> 25 -> 5 -> null
Dnull -> 5 -> 15 -> 25
Attempts:
2 left
💡 Hint
New nodes are added at the front, so the last pushed node is first.
🔧 Debug
advanced
2:00remaining
Why does this push method cause a compile error?
Examine the push method below. Why does it cause a compile error in Rust?
DSA Rust
fn push(&mut self, data: i32) {
    let new_node = Box::new(Node {
        data,
        next: self.head,
    });
    self.head = Some(new_node);
}
ACannot move out of borrowed content: self.head is borrowed and moved without using take()
BMissing semicolon after new_node declaration
CBox::new cannot be used inside a method
DData type mismatch between data and next fields
Attempts:
2 left
💡 Hint
Look at how self.head is used inside the method.
🧠 Conceptual
advanced
1:00remaining
What is the time complexity of pushing a node at the front of a singly linked list?
Consider a singly linked list where push inserts a new node at the front. What is the time complexity of this push operation?
AO(n^2) - Quadratic time
BO(1) - Constant time
CO(log n) - Logarithmic time
DO(n) - Linear time
Attempts:
2 left
💡 Hint
Pushing at the front does not require traversing the list.
🚀 Application
expert
2:30remaining
After pushing nodes, what is the value of the third node's data?
Using the push method that inserts nodes at the front, after pushing these values in order: 7, 14, 21, 28, what is the data value of the third node in the list?
DSA Rust
struct Node {
    data: i32,
    next: Option<Box<Node>>,
}

struct LinkedList {
    head: Option<Box<Node>>,
}

impl LinkedList {
    fn new() -> Self {
        LinkedList { head: None }
    }

    fn push(&mut self, data: i32) {
        let new_node = Box::new(Node {
            data,
            next: self.head.take(),
        });
        self.head = Some(new_node);
    }

    fn get_third_data(&self) -> Option<i32> {
        let mut current = &self.head;
        let mut count = 1;
        while let Some(node) = current {
            if count == 3 {
                return Some(node.data);
            }
            current = &node.next;
            count += 1;
        }
        None
    }
}

fn main() {
    let mut list = LinkedList::new();
    list.push(7);
    list.push(14);
    list.push(21);
    list.push(28);
    println!("Third node data: {:?}", list.get_third_data());
}
A28
B21
C7
D14
Attempts:
2 left
💡 Hint
Remember the order nodes are added at the front.

Practice

(1/5)
1. What does the push operation do in a linked list when using a node?
easy
A. Adds a new node at the beginning of the list
B. Removes the last node from the list
C. Searches for a node with a specific value
D. Deletes the entire list

Solution

  1. Step 1: Understand push operation in linked list

    The push operation adds a new node to the linked list.
  2. Step 2: Identify where the new node is added

    In a singly linked list, push usually adds the new node at the start (head) of the list.
  3. Final Answer:

    Adds a new node at the beginning of the list -> Option A
  4. Quick Check:

    Push = Add at head [OK]
Hint: Push always adds node at the start of the list [OK]
Common Mistakes:
  • Thinking push adds at the end
  • Confusing push with pop
  • Assuming push deletes nodes
2. Which of the following Rust code snippets correctly creates a new node and pushes it to the front of a linked list?
easy
A. head = Some(Node { data: 5, next: None });
B. let new_node = Node { data: 5, next: None }; head = new_node.next;
C. let new_node = Box::new(Node { data: 5, next: head }); head = Some(new_node);
D. let new_node = Box::new(Node { data: 5 }); head = new_node.next;

Solution

  1. Step 1: Check node creation with Box and linking

    let new_node = Box::new(Node { data: 5, next: head }); head = Some(new_node); creates a boxed node with data 5 and next pointing to current head.
  2. Step 2: Assign new node as new head

    Then head is updated to Some(new_node), correctly pushing at front.
  3. Final Answer:

    let new_node = Box::new(Node { data: 5, next: head }); head = Some(new_node); -> Option C
  4. Quick Check:

    Boxed node with next=head, then head=Some(new_node) [OK]
Hint: Use Box::new and set next to current head [OK]
Common Mistakes:
  • Not boxing the node
  • Not updating head correctly
  • Setting next to None instead of head
3. What will be the printed linked list after executing this Rust code?
struct Node {
    data: i32,
    next: Option,
}

fn push(head: &mut Option, data: i32) {
    let new_node = Box::new(Node { data, next: head.take() });
    *head = Some(new_node);
}

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

fn main() {
    let mut head = None;
    push(&mut head, 10);
    push(&mut head, 20);
    push(&mut head, 30);
    print_list(&head);
}
medium
A. 10 -> 20 -> 30 -> null
B. 30 -> 20 -> 10 -> null
C. null
D. Compilation error

Solution

  1. Step 1: Trace push calls and list building

    First push adds 10 as head. Second push adds 20 before 10. Third push adds 30 before 20.
  2. Step 2: Print list from head

    Print starts at head (30), then 20, then 10, then null.
  3. Final Answer:

    30 -> 20 -> 10 -> null -> Option B
  4. Quick Check:

    Last pushed node is head, so list is 30->20->10->null [OK]
Hint: Last pushed node appears first in list [OK]
Common Mistakes:
  • Assuming push adds at end
  • Confusing order of nodes
  • Ignoring Option and Box usage
4. Identify the error in this Rust function that tries to push a new node to the linked list head:
fn push(head: &mut Option, data: i32) {
    let new_node = Box::new(Node { data, next: head });
    *head = Some(new_node);
}
medium
A. Cannot assign &Option to next, must use head.take()
B. Missing return statement
C. Box::new cannot be used here
D. Data type i32 is incorrect

Solution

  1. Step 1: Analyze next field assignment

    The next field expects Option, but head is &mut Option, a reference.
  2. Step 2: Correct usage with head.take()

    Using head.take() moves the Option out, allowing ownership transfer to next.
  3. Final Answer:

    Cannot assign &Option to next, must use head.take() -> Option A
  4. Quick Check:

    Use head.take() to move ownership [OK]
Hint: Use head.take() to move Option ownership [OK]
Common Mistakes:
  • Assigning reference instead of owned Option
  • Ignoring Rust ownership rules
  • Thinking Box::new is invalid here
5. Given a linked list implemented with nodes in Rust, how would you modify the push function to prevent memory leaks when repeatedly pushing nodes?
hard
A. Use clone() on nodes to duplicate data
B. Use raw pointers instead of Box to manage nodes
C. Manually call drop() on old head before pushing
D. Use head.take() to transfer ownership and avoid dangling references

Solution

  1. Step 1: Understand ownership in Rust linked list

    Each node owns the next node via Box. To avoid leaks, ownership must be properly transferred.
  2. Step 2: Use head.take() to move ownership safely

    head.take() replaces head with None and moves ownership to new node's next, preventing leaks.
  3. Final Answer:

    Use head.take() to transfer ownership and avoid dangling references -> Option D
  4. Quick Check:

    head.take() safely moves ownership, preventing leaks [OK]
Hint: Use head.take() to transfer ownership safely [OK]
Common Mistakes:
  • Using raw pointers causing unsafe code
  • Calling drop manually causing double free
  • Cloning nodes causing unnecessary copies
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