题目描述
输入一个链表,按链表从尾到头的顺序返回一个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;
solution(head, res);
return res;
}
void solution(ListNode *head, vector<int> &res)
{
if(head == nullptr) return;
solution(head->next, res);
res.push_back(head->val);
}
};