题目
思路
代码
/**
* Definition for singly-linked list.
* function ListNode(val) {
* this.val = val;
* this.next = null;
* }
*/
/**
* @param {ListNode} head
* @return {number[]}
*/
var reversePrint = function(head) {
let ans = [];
if(!head){return ans;}
back(head);
return ans;
//回溯
function back(cur){
if(!cur){return;}
back(cur.next);
ans.push(cur.val);//这里每次压入栈的,都是上一层的node
}
};