一、题目描述
输入一个链表的头节点,从尾到头反过来返回每个节点的值(用数组返回)。
二、保姆级参考代码
// 登录 AlgoMooc 官网获取更多算法图解
// https://www.algomooc.com
// 作者:程序员吴师兄
class Solution {
public int[] reversePrint(ListNode head) {
// 构建一个栈,用来存储链表中每个节点的值
Stack<Integer> stack = new Stack<>();
// 构建一个指针,指向链表的头结点位置,从它开始向后遍历
ListNode curNode = head;
// 不断的遍历原链表中的每个节点,直到为 null
while (curNode != null){
// 把每个节点的值加入到栈中
stack.push(curNode.val);
// curNode 向后移动
curNode = curNode.next;
}
// 获取栈的长度
int size = stack.size();
// 通过栈的长度,定义一个同样长度的数组 res
int[] res = new int[size];
// 遍历栈,从栈顶挨个弹出每个值,把这些值依次加入到数组 res 中
for(int i = 0 ; i < size ; i++){
// 数组接收栈顶元素值
res[i] = stack.pop();
}
// 最后返回结果数组就行
return res;
}
}