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