题解 | #从尾到头打印链表# | Rust
从尾到头打印链表
https://www.nowcoder.com/practice/d0267f7f55b3412ba93bd35cfa8e8035
/** * #[derive(PartialEq, Eq, Debug, Clone)] * pub struct ListNode { * pub val: i32, * pub next: Option<Box<ListNode>> * } * * impl ListNode { * #[inline] * fn new(val: i32) -> Self { * ListNode { * val: val, * next: None, * } * } * } */ struct Solution{ } impl Solution { fn new() -> Self { Solution{} } /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * @param head ListNode类 * @return int整型一维数组 */ pub fn printListFromTailToHead(&self, head: Option<Box<ListNode>>) -> Vec<i32> { if head.is_none() { return vec![]; } let mut ans = Vec::new(); let mut head = head; ans.append(&mut Solution::printListFromTailToHead(self, head.as_mut().unwrap().next.take())); ans.push(head.as_ref().unwrap().val); return ans; } }