借助栈的性质:先进后出 来完成链表元素倒序的输出
将栈中一依次输出的元素,存储到数组内,返回该数组即可
class Solution
{
public:
vector<int> reversePrint(ListNode *head)
{
vector<int> a;
stack<int> b;
while(head)
{
b.push(head->val);
head = head->next;
}
while(!b.empty())
{
a.push_back(b.top());
b.pop();
}
return a;
}
};