题干
我的解答
function reversePrint(head: ListNode | null): number[] {
let nex = head;
let arr:number[] = [];
while(nex){
arr.unshift(nex.val);
nex = nex.next
}
return arr
};
示例代码
function reversePrint(head: ListNode | null): number[] {
const ans:number[] = [];
while(head) {
ans.push(head.val);
head = head.next;
}
return ans.reverse();
};
分析
//记录两个方法
arr.unshift(nex.val);//在数组头插入
ans.reverse(); //反转数组