递归:
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
class Solution {
ArrayList<Integer> list = new ArrayList<>();
public int[] reversePrint(ListNode head) {
if(head!=null){
reversePrint(head.next);
list.add(head.val);
}
//下面将列表转化为数组
int []res=new int[list.size()];
for(int i=0;i<res.length;i++)
res[i]=list.get(i);
return res;
}
}