offer-06


/**
输入一个链表的头节点,从尾到头反过来返回每个节点的值(用数组返回)。 

 

 示例 1: 

 输入:head = [1,3,2]
输出:[2,3,1] 

 

 限制: 

 0 <= 链表长度 <= 10000 
 Related Topics栈 | 递归 | 链表 | 双指针 

 👍 354, 👎 0 

*/
//leetcode submit region begin(Prohibit modification and deletion)
/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode(int x) { val = x; }
 * }
 */
class Solution {
    public int[] reversePrint(ListNode head) {
        int len = 0;
        ListNode node = head;
        while (node != null) {
            len++;
            node = node.next;
        }

        int[] result = new int[len];
        doReverse(head, result, len - 1);
        return result;
    }

    private void doReverse(ListNode node, int[] result, int curIndex) {
        if (node == null) {
            return;
        }

        doReverse(node.next, result, curIndex - 1);
        result[curIndex] = node.val;
    }
}
//leetcode submit region end(Prohibit modification and deletion)

文章作者: 倪春恩
版权声明: 本博客所有文章除特別声明外,均采用 CC BY 4.0 许可协议。转载请注明来源 倪春恩 !