#include <iostream>
#include <stack>
#include <vector>
using namespace std;
struct ListNode {
int val;
struct ListNode *next;
ListNode(int x):
val(x), next(NULL) {
}
};
class Solution {
public:
vector<int> printListFromTailToHead(ListNode* head) {
stack<int> nodes;
ListNode * pNode = head;
while(pNode != nullptr){
nodes.push(pNode->val);
pNode = pNode->next;
}
vector<int> res;
while(!nodes.empty()){
res.push_back(nodes.top());
//cout << nodes.top() <<endl;
nodes.pop();
}
return res;
}
void AddToTail(ListNode** pHead, int value){
ListNode * pNew = new ListNode(value);
ListNode * pNode = *pHead;
while(pNode->next != nullptr){
pNode = pNode->next;
}
pNode->next = pNew;
}
};
int main()
{
Solution s;
ListNode *p = new ListNode(1);
s.AddToTail(&p, 2);
s.AddToTail(&p, 3);
s.AddToTail(&p, 4);
s.AddToTail(&p, 5);
vector<int> res = s.printListFromTailToHead(p);
return 0;
}