输入一个链表,按链表从尾到头的顺序返回一个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>res;
if(head==NULL)
return res;
ListNode *p=head;
while(p!=nullptr)
{
res.push_back(p->val);
p=p->next;
}
//reverse(res.begin(),res.end());//直接输出res,就好,不用再建一个向量
vector<int>out;
for(auto iter=res.rbegin();iter!=res.rend();iter++)
{
out.push_back(*iter);
}
return out;
}
};