剑指offer-从尾到头打印链表
题目描述
输入一个链表的头节点,从尾到头反过来返回每个节点的值(用数组返回)。
思路:
没啥好说的
code
public int[] reversePrint(ListNode head) {
int cnt=0;
Stack<Integer> stack=new Stack<>();
while(head!=null) {
stack.push(head.val);
head=head.next;
cnt++;
}
int[] arr=new int[cnt];
int i=0;
while(!stack.empty()) {
arr[i++]=stack.pop();
}
return arr;
}