讨论/《图解算法数据结构》 - 剑指 Offer 06. 从尾到头打印链表/
《图解算法数据结构》 - 剑指 Offer 06. 从尾到头打印链表

java递归

class Solution {
    public int[] reversePrint(ListNode head) {
        return head == null ? new int[0] : helper(head, 0);
    }
    
    public int[] helper(ListNode head, int level){
        if (head.next == null){
            int[] curr = new int[level + 1];
            curr[0] = head.val;
            return curr;
        }
        int[] now = helper(head.next, level + 1);
        now[now.length - level - 1] = head.val;
        return now;
    }
}
1
展开全部 39 讨论