Challenge - 5 Problems
Linked List Length Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateWhat is the length of the linked list after these insertions?
Consider a singly linked list where nodes are inserted at the head. What will be the length of the list after inserting the values 10, 20, and 30 in that order?
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 insert_at_head(&mut self, data: i32) {
let new_node = Box::new(Node {
data,
next: self.head.take(),
});
self.head = Some(new_node);
}
fn length(&self) -> usize {
let mut count = 0;
let mut current = &self.head;
while let Some(node) = current {
count += 1;
current = &node.next;
}
count
}
}
fn main() {
let mut list = LinkedList::new();
list.insert_at_head(10);
list.insert_at_head(20);
list.insert_at_head(30);
println!("Length: {}", list.length());
}Attempts:
2 left
💡 Hint
Each insertion adds one node at the head, increasing the length by one.
✗ Incorrect
The list starts empty. Each insert_at_head adds a new node at the front. After inserting 10, length is 1; after 20, length is 2; after 30, length is 3.
❓ Predict Output
intermediateWhat is the output of length method on an empty linked list?
Given a newly created linked list with no nodes, what will be the output of calling the length method?
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 length(&self) -> usize {
let mut count = 0;
let mut current = &self.head;
while let Some(node) = current {
count += 1;
current = &node.next;
}
count
}
}
fn main() {
let list = LinkedList::new();
println!("Length: {}", list.length());
}Attempts:
2 left
💡 Hint
An empty list has no nodes, so length should be zero.
✗ Incorrect
The head is None, so the while loop never runs and count remains zero.
🔧 Debug
advancedWhy does this length method cause a compile error?
This Rust code for length method in linked list causes a compile error. What is the cause?
DSA Rust
fn length(&self) -> usize {
let mut count = 0;
let mut current = self.head;
while let Some(node) = current {
count += 1;
current = node.next;
}
count
}Attempts:
2 left
💡 Hint
In Rust, you cannot move ownership from a borrowed reference.
✗ Incorrect
The code tries to move self.head out of &self, which is not allowed. Instead, it should borrow references to traverse.
🧠 Conceptual
advancedWhat is the time complexity of the length method for a singly linked list?
Given a singly linked list with n nodes, what is the time complexity of a method that counts the number of nodes by traversing the list?
Attempts:
2 left
💡 Hint
The method visits each node once.
✗ Incorrect
The length method must check each node once, so it takes time proportional to n nodes.
❓ Predict Output
expertWhat is the printed output after reversing the linked list and getting its length?
Given the linked list with nodes 1 -> 2 -> 3 -> null, after reversing it and then getting its length, what is the output?
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 insert_at_head(&mut self, data: i32) {
let new_node = Box::new(Node {
data,
next: self.head.take(),
});
self.head = Some(new_node);
}
fn reverse(&mut self) {
let mut prev = None;
let mut current = self.head.take();
while let Some(mut node) = current {
let next = node.next.take();
node.next = prev;
prev = Some(node);
current = next;
}
self.head = prev;
}
fn length(&self) -> usize {
let mut count = 0;
let mut current = &self.head;
while let Some(node) = current {
count += 1;
current = &node.next;
}
count
}
fn print_list(&self) {
let mut current = &self.head;
while let Some(node) = current {
print!("{} -> ", node.data);
current = &node.next;
}
println!("null");
}
}
fn main() {
let mut list = LinkedList::new();
list.insert_at_head(3);
list.insert_at_head(2);
list.insert_at_head(1);
list.reverse();
list.print_list();
println!("Length: {}", list.length());
}Attempts:
2 left
💡 Hint
Reversing the list changes the order of nodes, but length remains the same.
✗ Incorrect
Initially list is 1 -> 2 -> 3 -> null. After reverse, it becomes 3 -> 2 -> 1 -> null. Length is still 3.
