题目描述
输入一个链表,按链表从尾到头的顺序返回一个ArrayList。
看到书上有推荐使用递归,或者栈来接收的,这里我使用vector:
class Solution {
public:
vector<int> printListFromTailToHead(ListNode* head) {
vector<int> res;
while(head!=NULL) {
res.push_back(head->val);
head=head->next;
}
reverse(res.begin(),res.end());
return res;
}
};