链表:剑指 Offer 06. 从尾到头打印链表

题目描述:

 

方法:递归法

 

class Solution{
    ArrayList<Integer> tmp = new ArrayList<>();
    public int[] reversePrint(ListNode head){
        recur(head);
        int res[] = new int[tmp.size()];
        for(int i=0;i<res.length;i++){
            res[i] = tmp.get(i);
        }
        return res;
    }
    void recur(ListNode head){
        if(head==null) return;
        recur(head.next);//递推阶段
        tmp.add(head.val);//回溯阶段将当前节点值加入列表
    }
}

 

posted @ 2023-06-05 09:53  ZDREAMER  阅读(1)  评论(0编辑  收藏  举报