剑指offer-从尾到头打印链表

题目描述

输入一个链表,按链表值从尾到头的顺序返回一个ArrayList。
/**
*  struct ListNode {
*        int val;
*        struct ListNode *next;
*        ListNode(int x) :
*              val(x), next(NULL) {
*        }
*  };
*/
class Solution {
public:
    vector<int> printListFromTailToHead(ListNode* head) {
        vector<int> v;
        while(head != nullptr)
        {
            v.push_back(head->val);
            head = head->next;
        }
        reverse(v.begin(),v.end());
        return v;
    }
};
原文地址:https://www.cnblogs.com/Jawen/p/10960203.html