采用翻转链表
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
class Solution {
public int[] reversePrint(ListNode head) {
// 翻转链表
if (head == null) {
return new int[]{};
}
ListNode pre = null;
ListNode cur = head;
int count = 0, i = 0;
while (cur != null) {
count++;
ListNode next = cur.next;
cur.next = pre;
pre = cur;
cur = next;
}
int[] arr = new int[count];
while (pre != null) {
arr[i++] = pre.val;
pre = pre.next;
}
return arr;
}
}
采用辅助栈