<剑指Offer>
输入一个链表,按链表值从尾到头的顺序返回一个ArrayList。
/* struct ListNode {
int val ;
struct ListNode *next;
ListNode(int x) { val(x), next(NULL) }
}; */
class Solution {
public:
vector printListFromTailToHead(ListNode* head) {
ListNode *p = head;
vector v;
stack s;
while(p != NULL)
{
s.push(p->val);
p = p->next;
}
while(!s.empty())
{
v.push_back(s.top());
s.pop();
}
return v;
}
};