day3
输入一个链表的头节点,从尾到头反过来返回每个节点的值(用数组返回)。
示例 1:
输入:head = [1,3,2] 输出:[2,3,1]
class ListNode { val: number; next: ListNode | null; constructor(val?: number, next?: ListNode | null) { this.val = val === undefined ? 0 : val; this.next = next === undefined ? null : next; } } function reversePrint(head: ListNode | null): number[] { return head == null ? [] : reversePrint(head.next).concat(head.val); } const list: number[] = [ 2, 3, 1 ]; //reversePrint(3).concat(2); //reversePrint(reversePrint(1).concat(3);).concat(2); //(reversePrint(reversePrint(reversePrint(null).concat(1)).concat(3)).concat(2); //(([].concat(1)).concat(3)).concat(2) //([1].concat(3)).concat(2) //[1,3].concat(2) //[1,3,2]