rust_test/src/list.rs

33 lines
726 B
Rust

// Definition for singly-linked list.
#[derive(PartialEq, Eq, Clone, Debug)]
pub struct ListNode {
pub val: i32,
pub next: Option<Box<ListNode>>
}
impl ListNode {
#[inline]
fn new(val: i32) -> Self {
ListNode {
next: None,
val
}
}
}
pub fn make_list(arr: Vec<i32>) -> Option<Box<ListNode>> {
let mut head: Option<Box<ListNode>> = None;
let mut current = &mut head;
for &value in arr.iter() {
let new_node = Box::new(ListNode::new(value));
if current.is_none() {
*current = Some(new_node);
} else {
current.as_mut().unwrap().next = Some(new_node);
current = &mut current.as_mut().unwrap().next;
}
}
head
}