Challenge - 5 Problems
Linked List Push Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateWhat 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();
}Attempts:
2 left
💡 Hint
Remember that pushing adds a new node at the front of the list.
✗ Incorrect
Each push adds a new node at the front, so the last pushed node appears first when printing.
❓ Predict Output
intermediateWhat 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();
}Attempts:
2 left
💡 Hint
New nodes are added at the front, so the last pushed node is first.
✗ Incorrect
Pushing 5, then 15, then 25 results in 25 at the head, followed by 15, then 5.
🔧 Debug
advancedWhy 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);
}Attempts:
2 left
💡 Hint
Look at how self.head is used inside the method.
✗ Incorrect
self.head is an Option>. Moving it directly causes a borrow error. Using self.head.take() replaces it with None and moves the value safely.
🧠 Conceptual
advancedWhat 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?
Attempts:
2 left
💡 Hint
Pushing at the front does not require traversing the list.
✗ Incorrect
Pushing at the front only changes the head pointer and the new node's next pointer, which takes constant time.
🚀 Application
expertAfter 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());
}Attempts:
2 left
💡 Hint
Remember the order nodes are added at the front.
✗ Incorrect
Nodes are pushed at the front, so the list is 28 -> 21 -> 14 -> 7. The third node has data 14, but counting from head: 1=28, 2=21, 3=14. So the third node's data is 14.
