示例 1:
输入:head = [1,3,2] //链表
输出:[2,3,1]
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
class Solution {
public int[] reversePrint(ListNode head) {
//双端队列初始化
Deque<Integer> dq = new LinkedList<>();
ListNode p = head;//相当于新建了链表指针
int len=0;
while(p!=null){//判是否为空即可
//添加到队头
dq.offerFirst(p.val);
len++;
p = p.next;
}
int[] nums=new int[len];
for(int i = 0;i < len ;i++){
nums[i] = dq.peekFirst();//读取队头
dq.removeFirst();//弹出队头
}
return nums;
}
}