这个题很简单,用一个栈就可以完美解决。
/**
* 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> nums;
stack<int> s;
while(head!=NULL)
{
s.push(head->val);
head=head->next;
}
while(s.size()!=0)
{
nums.push_back(s.top());
s.pop();
}
return nums;
}
};