跟书上有一点不同的是,书上是直接打印,而题目要求返回一个数组
所以要新建一个数组
class Solution {
public:
vector<int> res;
vector<int> reversePrint(ListNode* head) {
stack<int> st;
ListNode* pNode=head;
while(pNode)
{
st.push(pNode->val);
pNode=pNode->next;
}
while(!st.empty())
{
res.push_back(st.top());
st.pop();
}
return res;
}
};