題目描述
https://leetcode-cn.com/problems/cong-wei-dao-tou-da-yin-lian-biao-lcof/
參考
復(fù)雜度
時間:n
空間:n
代碼
/**
* 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> st;
while(head){
st.push(head->val);
head=head->next;
}
vector<int> ans;
while(!st.empty()){
ans.push_back(st.top());
st.pop();
}
return ans;
}
};