剑指 Offer 06. 从尾到头打印链表

输入一个链表的头节点,从尾到头反过来返回每个节点的值(用数组返回)。

思路:

设置一个辅助栈,利用栈的特性逆向

复杂度:

​ O(1)

题解:

class Solution {
public:
vector<int> reversePrint(ListNode* head) {
vector<int> res;
stack<int> sup;
while(head!=NULL){
sup.push(head->val);
head=head->next;
}
int i=0;
while(!sup.empty()){
res.push_back(sup.top());
sup.pop();
}
return res;
}
};