剑指offer-03-从尾到头打印链表

题目描述

输入一个链表,按链表值从尾到头的顺序返回一个ArrayList。

题目分析

数组,头尾,push(尾插),unshif(头插),尾到头,使用unshift。

代码

/*function ListNode(x){
    this.val = x;
    this.next = null;
}*/
function printListFromTailToHead(head)
{
    // write code here
    let res = [],p=head;
    while(head != null){
        res.unshift(head.val);
        p=p.next;
    }
    return res;
}
原文地址:https://www.cnblogs.com/ShineaSYR/p/9532136.html