Loading

JS_剑指 Offer 06. 从尾到头打印链表

逆序输出这个链表,返回的是数组,可以用数组的unshit

/**
 * Definition for singly-linked list.
 * function ListNode(val) {
 *     this.val = val;
 *     this.next = null;
 * }
 */
/**
 * @param {ListNode} head
 * @return {number[]}
 */
var reversePrint = function(head) {
    let nums = [];
    let node = head;
    while(node != null){
        nums.unshift(node.val);
        node = node.next;
    }
    return nums;
};
posted @ 2021-05-11 20:01  想用包子换论文  阅读(65)  评论(0)    收藏  举报