题目描述
输入一个链表,按链表值从尾到头的顺序返回一个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> ret;
stack<int> nodes;
ListNode* cur = head;
while(cur){
nodes.push(cur->val);
cur = cur->next;
}
int size = nodes.size();
while(size--){
ret.push_back(nodes.top());
nodes.pop();
}
return ret;
}
};