1.题目描述
输入一个链表的头节点,从尾到头反过来返回每个节点的值(用数组返回)。

2.算法分析
倒序输出,使用栈Stack的数据结构。
先将链表中的元素入栈,然后遍历栈内元素,将元素加入到数组中。
3.代码实现
/*** Definition for singly-linked list.* public class ListNode {* int val;* ListNode next;* ListNode(int x) { val = x; }* }*/
class Solution {public int[] reversePrint(ListNode head) {Stack<ListNode> stack = new Stack<>();while(head != null){stack.push(head);head = head.next;} int[] arr = new int[stack.size()];int index = 0;while(!stack.isEmpty()){arr[index++] = stack.pop().val;}return arr;}
}















