java刷题--剑指offer06 从头到尾打印链表
题目
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/9e6c1ddc0c5b92dadd5ea53d8a392041.png)
代码
class Solution {
public int[] reversePrint(ListNode head) {
Stack<ListNode> stack = new Stack();
ListNode pre = head;
while(pre!=null) {
stack.push(pre);
pre = pre.next;
}
int[] res = new int[stack.size()];
int i=0;
while(!stack.isEmpty()) {
res[i++] = stack.pop().val;
}return res;
}
}
结果
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/a82e1869964c77ba984900b9699c9e8a.png)