剑指 Offer 06. 从尾到头打印链表
逆转vector数组
class Solution {
public:
vector<int> reversePrint(ListNode* head) {
vector<int>res;
while(head){
res.push_back((*head).val);
head=(*head).next;
}
reverse(res.begin(),res.end());
return res;
}
};
辅助栈
class Solution {
public:
vector<int> reversePrint(ListNode* head) {
vector<int>res;
stack<int> a;
while(head){
a.push((*head).val);
head=(*head).next;
}
while(!a.empty()){
res.push_back(a.top());
a.pop();
}
return res;
}
};