题目描述
输入一个链表,从尾到头打印链表每个节点的值
class Solution {
public:
vector<int> printListFromTailToHead(ListNode* head) {
vector<int> rec;
if(head==NULL)
return rec;
stack<int> mStack;
ListNode* p = head;
while(p != NULL)
{
mStack.push(p->val);
p = p->next;
}
while(!mStack.empty())
{
rec.push_back(mStack.top());
mStack.pop();
}
return rec;
}
};