#### [剑指 Offer 06. 从尾到头打印链表](https://leetcode-cn.com/problems/cong-wei-dao-tou-da-yin-lian-biao-lcof/)

难度 简单

输入一个链表的头节点，从尾到头反过来返回每个节点的值（用数组返回）。

 

**示例 1：**

```
输入：head = [1,3,2]
输出：[2,3,1]
```

 

**限制：**

```
0 <= 链表长度 <= 10000
```

#### 解法

```java
//递归
class Solution {
    public int[] reversePrint(ListNode head) {
        if (head == null) return new int[0]; 
        List<Integer> list = new ArrayList<>();
        helper(list, head);
        int size = list.size();
        int[] ret = new int[size];        
        for (int i = 0; i < size; i++) {
            ret[i] = list.get(i);
        }
        return ret;
    }
    private void helper(List<Integer> list, ListNode head) {
        if (head == null) return;
        helper(list, head.next);
        list.add(head.val);
    }  
}

//辅助栈
class Solution {
     public int[] reversePrint(ListNode head) {
        if (head == null) return new int[0]; 
        Deque<Integer> stack = new ArrayDeque<>();
        while (head != null) {
            stack.push(head.val);
            head = head.next;
        }
        int size = stack.size();
        int[] ret = new int[size];        
        for (int i = 0; i < size; i++) {
            ret[i] = stack.pop();
        }
        return ret;
    }
}
```

