1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
/// 剑指 Offer 06. 从尾到头打印链表
/// <https://leetcode.cn/problems/cong-wei-dao-tou-da-yin-lian-biao-lcof/>

pub struct Solution;

// 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 }
	}
}

impl Solution {
	// pub fn reverse_print(head: Option<Box<list_node::ListNode>>) -> Vec<i32> leetcode
	// 上提交需要改成这行
	pub fn reverse_print(head: Option<Box<ListNode>>) -> Vec<i32> {
		let mut result = vec![];
		let mut node = head;
		while let Some(n) = node {
			result.push(n.val);
			node = n.next;
		}
		result.reverse();
		result
	}
}

#[cfg(test)]
mod tests {
	use super::*;

	#[test]
	fn test() {
		assert_eq!(Solution::reverse_print(None), vec![]);
		assert_eq!(
			Solution::reverse_print(Some(Box::new(ListNode { val: 2, next: None }))),
			vec![2]
		);
		assert_eq!(
			Solution::reverse_print(Some(Box::new(ListNode {
				val: 1,
				next: Some(Box::new(ListNode {
					val: 3,
					next: Some(Box::new(ListNode { val: 2, next: None })),
				})),
			}))),
			vec![2, 3, 1]
		);
	}
}