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