/**
* 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)
{
vector<int> ans;
ListNode* cur = head;
while(cur!= nullptr)
{
ans.push_back(cur->val);
cur = cur->next;
}
reverse(ans.begin(),ans.end());
return ans;
}
};
、、、、、、、、、、、、、、、、、、、、、、、、、
class Solution {
public:
vector<int> reversePrint(ListNode* head)
{
vector<int> res;
stack<int> s;
//入栈
while(head){
s.push(head->val);
head = head->next;
}
//出栈
while(!s.empty()){
res.push_back(s.top());
s.pop();
}
return res;
}
};