class Solution {
public int[] printListReversingly(ListNode head) {
if (head == null) {
return null;
}
if (head.next == null) {
return new int[] {head.val} ;
}
ListNode prev = null;
ListNode next;
int count = 0;
while (head != null) {
next = head.next;
head.next = prev;
prev = head;
head = next;
count++;
}
int[] ans = new int[count];
for (int i = 0; i < count && prev != null; i++, prev = prev.next) {
ans[i] = prev.val;
}
return ans;
}
}