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

class Solution {
    public int[] reversePrint(ListNode head) {
        if (head == null) {
            return new int[0];
        }

        ListNode origin = head;
        int counter = 1;

        while (head.next != null) {
            counter++;
            head = head.next;
        }

        int[] is = new int[counter];

        for (int i = counter - 1; i >= 0; i--) {
            is[i] = origin.val;
            origin = origin.next;
        }

        return is;
    }
}
image.png

你可能感兴趣的:(剑指 Offer 06. 从尾到头打印链表)