leetcode/
reverse_print.rs1use crate::ListNode;
21
22pub struct Solution;
23
24impl Solution {
25 pub fn reverse_print(head: Option<Box<ListNode>>) -> Vec<i32> {
26 head.map(|mut head| {
27 if head.next.is_some() {
28 let mut v = Solution::reverse_print(head.next.take());
29 v.push(head.val);
30 v
31 } else {
32 vec![head.val]
33 }
34 }).unwrap_or(vec![])
35 }
36}
37
38#[cfg(test)]
39mod tests {
40 use super::*;
41 use crate::list;
42
43 #[test]
44 fn test() {
45 let cases = vec![
46 (vec![2,3,1], list![1,3,2]),
47 (vec![], list![]),
48 ];
49 for (expect, input) in cases {
50 assert_eq!(expect, Solution::reverse_print(input));
51 }
52 }
53}