思路
这道题题解有很多解法,第一次看下去,就感觉用栈,比较容易理解,把head的值压入栈,再把栈弹出。但是要用数组方式返回,这里题解是使用容器vector的方式。这点我没想到,还是对C++的语言不熟悉吧
/**
* 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) {
stack<int>s;
vector<int> res;
ListNode*pre=head;
while(pre){
s.push(pre->val);
pre=pre->next;
}
while(!s.empty()){
res.push_back(s.top());
s.pop();
}
return res;
}
};