3. Given the following Rust code snippet for reversing a singly linked list iteratively, what will be the printed output after reversal?
struct Node {
val: i32,
next: Option,
}
fn print_list(head: &Option) {
let mut current = head;
while let Some(node) = current {
print!("{} -> ", node.val);
current = &node.next;
}
println!("null");
}
fn reverse_list(mut head: Option) -> Option {
let mut prev = None;
while let Some(mut current) = head {
head = current.next.take();
current.next = prev;
prev = Some(current);
}
prev
}
fn main() {
let node3 = Node { val: 3, next: None };
let node2 = Node { val: 2, next: Some(Box::new(node3)) };
let node1 = Node { val: 1, next: Some(Box::new(node2)) };
let head = Some(Box::new(node1));
let reversed_head = reverse_list(head);
print_list(&reversed_head);
}