题目描述
输入一个链表,按链表从尾到头的顺序返回一个ArrayList。
/**
* struct ListNode {
* int val;
* struct ListNode *next;
* ListNode(int x) :
* val(x), next(NULL) {
* }
* };
*/
class Solution {
public:
std::vector<int> printListFromTailToHead(ListNode* head) {
std::vector<int> res;
while(head!=NULL)
{
res.emplace_back(head->val);
head=head->next;
}
reverse(res.begin(),res.end());
return res;
}
};