leetcode/src/util/linked_list.rs

30 lines
660 B
Rust
Raw Normal View History

2023-12-12 18:40:24 +08:00
#[derive(PartialEq, Eq, Debug)]
pub struct ListNode {
pub val: i32,
pub next: Option<Box<ListNode>>,
}
impl ListNode {
#[inline]
pub fn new(val: i32) -> Self {
ListNode { next: None, val }
}
}
// helper function for test
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()), *])};
}