首页 > 其他 > 详细

[剑指offer] 从尾到头打印链表

时间:2017-11-25 15:52:35      阅读:201      评论:0      收藏:0      [点我收藏+]

题目描述

输入一个链表,从尾到头打印链表每个节点的值。

没什么难度,看清从尾到头即可...

/**
*  struct ListNode {
*        int val;
*        struct ListNode *next;
*        ListNode(int x) :
*              val(x), next(NULL) {
*        }
*  };
*/
class Solution {
public:
    vector<int> printListFromTailToHead(ListNode* head) {
        vector<int> re;
        while (head) {
            re.push_back(head->val);
            head = head->next;
        }
        for (int i = 0; i < re.size() / 2; i++) swap(re[i], re[re.size() - 1 - i]);
        return re;
    }
};

 

[剑指offer] 从尾到头打印链表

原文:http://www.cnblogs.com/zmj97/p/7895392.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!