$T(n) = O(n), S(n) = O(n)$
/**
* Definition for singly-linked list.
* class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
class Solution {
public int[] printListReversingly(ListNode head) {
ListNode node = head;
int count = 0;
while (node != null) {
node = node.next;
count++;
}
int[] res = new int[count];
node = head;
for (int i = count - 1; i >= 0; i--) {
res[i] = node.val;
node = node.next;
}
return res;
}
}