1 /**
2 * public class ListNode {
3 * int val;
4 * ListNode next = null;
5 *
6 * ListNode(int val) {
7 * this.val = val;
8 * }
9 * }
10 *
11 */
12 import java.util.ArrayList;
13 import java.util.Stack;
14 public class Solution {
15 /*ArrayList<Integer> ai = new ArrayList<Integer>();//新建一个ArrayList的值存储值
16 public ArrayList<Integer> printListFromTailToHead(ListNode listNode) {
17 if(listNode != null) {
18 //递归调用
19 printListFromTailToHead(listNode.next);
20 ai.add(listNode.val);
21 }
22 return ai;
23 }*/
24 public ArrayList<Integer> printListFromTailToHead(ListNode listNode) {
25 ArrayList<Integer> ai = new ArrayList<Integer>();
26 Stack<Integer> stack = new Stack<Integer>();
27 while(listNode != null){
28 stack.push(listNode.val);
29 listNode = listNode.next;
30 }
31 while(!stack.isEmpty()){
32 ai.add(stack.pop());
33 }
34 return ai;
35 }
36 }