题目描述:
输入一个链表,按链表从尾到头的顺序返回一个ArrayList。
题解:
class Solution {
public:
vector<int> res;
vector<int> printListFromTailToHead(ListNode* head)
{
if(head == NULL)
return res;
help(head);
return res;
}
void help(ListNode * head)
{
if(head == NULL)
return;
help(head->next);
res.push_back(head->val);
}
};
解题思路:
链表版的后序遍历。尾递归。