输入一个链表的头节点,从尾到头反过来返回每个节点的值(用数组返回)。
    示例 1:
    输入:head = [1,3,2]
    输出:[2,3,1]
    限制:0 <= 链表长度 <= 10000
    方法一:(递归)

    1. /**
    2. * Definition for singly-linked list.
    3. * function ListNode(val) {
    4. * this.val = val;
    5. * this.next = null;
    6. * }
    7. */
    8. /**
    9. * @param {ListNode} head
    10. * @return {number[]}
    11. */
    12. var reversePrint = function(head) {
    13. if(head==null) return [];
    14. return [...reversePrint(head.next),head.val]
    15. };

    方法二:(unshift)

    1. /**
    2. * Definition for singly-linked list.
    3. * function ListNode(val) {
    4. * this.val = val;
    5. * this.next = null;
    6. * }
    7. */
    8. /**
    9. * @param {ListNode} head
    10. * @return {number[]}
    11. */
    12. var reversePrint = function(head) {
    13. let res = []
    14. while(head){
    15. res.unshift(head.val)
    16. head = head.next
    17. }
    18. return res
    19. };