classSolution{privateStack<Integer> stack =newStack<>();privateint[] res;publicint[]reversePrint(ListNode head){while(head !=null){//链表节点值依次入栈
stack.push(head.val);
head = head.next;}
res =newint[stack.size()];for(int i =0; i < res.length; i++){
res[i]= stack.pop();//依次出栈并写入数组中}return res;}}
递归法
classSolution{privateArrayList<Integer> list =newArrayList<Integer>();publicint[]reversePrint(ListNode head){recur(head);//调用递归方法int[] res =newint[list.size()];for(int i =0; i < res.length; i++){
res[i]= list.get(i);}return res;}voidrecur(ListNode head){//递归调用至链表末尾 if(head ==null){return;}recur(head.next);//由末端开始添加节点值至列表中
list.add(head.val);}}