1. 程式人生 > >劍指offer(3)從尾到頭打印鏈表

劍指offer(3)從尾到頭打印鏈表

() pre code == nod offer ever 順序 輸入

題目描述:

輸入一個鏈表,按鏈表值從尾到頭的順序返回一個ArrayList。

解題代碼:

/*function ListNode(x){
    this.val = x;
    this.next = null;
}*/
function printListFromTailToHead(head)
{
    // write code here
    var arr = [];
    if(head == null){
        return arr;
    }
    while(head != null){
        arr.push(head.val);
        head 
= head.next; } return arr.reverse(); }

劍指offer(3)從尾到頭打印鏈表