利用栈
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;
vector<int> result;
ListNode *p = head;
while (p != NULL) {
nodes.push(p->val);
p = p->next;
}
while (!nodes.empty()) {
result.push_back(nodes.top());
nodes.pop();
}
return result;
}
};