algo/rust/07_linkedlist/util/linked_list.rs

33 lines
704 B
Rust
Raw Permalink Normal View History

// 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 to_list(vec: Vec<i32>) -> Option<Box<ListNode>> {
let mut current = None;
for &v in vec.iter().rev() {
let mut node = ListNode::new(v);
node.next = current;
current = Some(Box::new(node));
}
current
}
#[macro_export]
macro_rules! linked {
($($e:expr),*) => {to_list(vec![$($e.to_owned()), *])};
($($e:expr,)*) => {to_list(vec![$($e.to_owned()), *])};
}