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

剑指OFFER 从尾到头打印链表

时间:2020-01-11 13:27:23      阅读:76      评论:0      收藏:0      [点我收藏+]

标签:cpp   empty   描述   tno   public   ack   from   back   返回   

剑指OFFER 从尾到头打印链表

题目描述:输入一个链表,按链表从尾到头的顺序返回一个ArrayList。

/**
*  struct ListNode {
*        int val;
*        struct ListNode *next;
*        ListNode(int x) :
*              val(x), next(NULL) {
*        }
*  };
*/
class Solution {
public:
    vector<int> printListFromTailToHead(ListNode* head) {
        stack<int> s;
        ListNode* node = head;
        while(node != NULL)
        {
            s.push(node->val);
            node = node->next;
        }
        vector<int> res;
        while(!s.empty())
        {
            res.push_back(s.top());
            s.pop();
        }
        return res;
    }
};

剑指OFFER 从尾到头打印链表

标签:cpp   empty   描述   tno   public   ack   from   back   返回   

原文地址:https://www.cnblogs.com/virgildevil/p/12179450.html

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