码迷,mamicode.com
首页 > 其他好文 > 详细

从尾到头打印链表

时间:2017-12-19 15:19:15      阅读:166      评论:0      收藏:0      [点我收藏+]

标签:next   title   nod   div   链表   node   cto   bsp   输入   

题目描述

输入一个链表,从尾到头打印链表每个节点的值
class Solution {
public:
    vector<int> printListFromTailToHead(ListNode* head) {
        vector<int> res;
        vector<int> stack;
        while(head)
        {
            stack.push_back(head->val);
            head=head->next;
        }
        for(int idx=stack.size()-1; idx>=0; --idx)
        {
            res.push_back(stack[idx]);
        }
        return res;
    }
};

 

从尾到头打印链表

标签:next   title   nod   div   链表   node   cto   bsp   输入   

原文地址:http://www.cnblogs.com/jeysin/p/8065048.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!