1. 程式人生 > 其它 >劍指offer--簡單題 --叢尾到頭列印連結串列

劍指offer--簡單題 --叢尾到頭列印連結串列

在這裡插入圖片描述

/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode(int x) { val = x; }
 * }
 */
class Solution {
    public int[] reversePrint(ListNode head) {
        Stack<Integer> stack  = new Stack<>();
        while(head!=null){
            stack.push(head.val);
            head = head.next;
        }
        int res[] = new int[stack.size()];
        for(int i = 0 ; i < res.length; i++){
            res[i] = stack.pop();
        }
        return res;
    }    
}