1. 程式人生 > >《劍指offer》從尾到頭列印連結串列

《劍指offer》從尾到頭列印連結串列

題目描述

輸入一個連結串列,按連結串列值從尾到頭的順序返回一個ArrayList。

/**
*  struct ListNode {
*        int val;
*        struct ListNode *next;
*        ListNode(int x) :
*              val(x), next(NULL) {
*        }
*  };
*/
class Solution {
public:
    vector<int> printListFromTailToHead(ListNode* head) {
        vector<int>value;
        if(head != NULL){
            value.insert(value.begin(), head->val);
            while(head->next != NULL){
                value.insert(value.begin(),head->next->val);
                head = head->next;
            }
        }
        return value;
    }
};