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

题目描述

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

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

/**
*  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;
    }
};
原文地址:https://www.cnblogs.com/zmj97/p/7895392.html