从尾到头打印链表

  vector<int> printListFromTailToHead(ListNode* head) {
        vector<int> res;
        
        if(!head)
            return res;
        
        ListNode *p = head;
        
        while(p) {
            res.push_back(p->val);
            p = p->next;
        }
        
        reverse(res.begin(),res.end());
        return res;
        
            
    }

 

posted on 2017-02-28 02:56  123_123  阅读(91)  评论(0编辑  收藏  举报