解题思路
使用stack辅助,先把值压进stack中,再从stack里把值压进vector
源代码:
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
vector<int> reversePrint(ListNode* head) {
ListNode *p = head;
vector<int> res;
if(head == NULL) return res;
stack<int> tmp_s;
while(p){
tmp_s.push(p->val);
p = p->next;
}
while(!tmp_s.empty()){
res.push_back(tmp_s.top());
tmp_s.pop();
}
return res;
}
};