思路:是用栈结构,利用先进后出的特性。
/**
* struct ListNode {
* int val;
* struct ListNode *next;
* ListNode(int x) :
* val(x), next(NULL) {
* }
* };
*/
class Solution {
public:
vector<int> printListFromTailToHead(ListNode* head) {
stack<int> s;
while(head != NULL)
{
s.push(head->val);
head = head->next;
}
vector<int> vec;
while(!s.empty())
{
vec.push_back(s.top());
s.pop();
}
return vec;
}
};