题目描述
/*
* Definition for singly-linked list.
* class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
/
class Solution {
public int[] printListReversingly(ListNode head) {
ArrayList<Integer> stack = new ArrayList<Integer>();
int len = 0;
while(head!= null){ // 目前的是空,而不是下一个为空
len++;
stack.add(head.val);
head = head.next;
}
int[] res = new int[len];
for(int i = 0; i < len; i++){
res[len-i-1] = stack.remove(0);
}
return res;
}
}