题目链接:https://leetcode-cn.com/problems/cong-wei-dao-tou-da-yin-lian-biao-lcof/
/**
* 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[0];
if(head.next==null) return new int[]{head.val};
List<Integer> list=new ArrayList<Integer>();
addNumbers(head,list);
int[] reverseArray=new int[list.size()];
for(int i=0;i<list.size();i++){
reverseArray[i]=list.get(i);
}
return reverseArray;
}
public void addNumbers(ListNode head,List<Integer> list){
if(head.next!=null) addNumbers(head.next,list);
list.add(head.val);
}
}