劍指offer--簡單題 --叢尾到頭列印連結串列
阿新 • • 發佈:2021-05-21
/** * 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; } }