/**
*    public class ListNode {
*        int val;
*        ListNode next = null;
*
*        ListNode(int val) {
*            this.val = val;
*        }
*    }
*
*/
import java.util.ArrayList;
public class Solution {
    public ArrayList<Integer> printListFromTailToHead(ListNode listNode) {
        ArrayList<Integer> res = new ArrayList();
        ListNode cur = listNode;
        while(cur != null){
            res.add(0,cur.val);
            cur = cur.next;
        }
        return res;
    }
}

这个递归会出错

import java.util.ArrayList;
public class Solution {
    ArrayList list = new ArrayList();
    public ArrayList printListFromTailToHead(ListNode listNode) {
        dfs(listNode);
        return list;
    }

    public void dfs(ListNode listNode){
        if(listNode == null){
            return;
        }
        dfs(listNode.next);
        list.add(listNode.val);
    }
}

递归方法2

import java.util.*;
public class Solution {
    ArrayList<Integer> list = new ArrayList();
    public ArrayList<Integer> printListFromTailToHead(ListNode listNode) {
        if(listNode!=null){
            printListFromTailToHead(listNode.next);
            list.add(listNode.val);
        }
        return list;
    }
}