无需修改链表,第一次遍历求出链表长度,第二次遍历依次将链表元素值从后往前存入到数组中
public int[] printListReversingly(ListNode head) {
int len = 0;
ListNode p = head;
while (p != null) {
len++;
p = p.next;
}
p = head;
int[] res = new int[len];
while (p != null) {
res[--len] = p.val;
p = p.next;
}
return res;
}