从尾到头打印链表

115 阅读1分钟

image.png

原题链接

解题思路:无

class Solution {
    public int[] reversePrint(ListNode head) {
        int count = 0 ;
        ListNode node = head;
        while(node != null){
            count ++;
            node = node.next;
        }
        int[] nums = new int[count];
        node = head;
        for(int i = count -1;i>=0;i--){
            nums[i] = node.val;
            node = node.next;
        }
        return nums;
    }
}