33 lines
704 B
Rust
33 lines
704 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 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()), *])};
|
||
|
}
|