思路整理 :1. 遍历一遍链表存入vector中
2.利用vector (v.rbegin(),v.rend()) 就可以实现逆序输出了
当然也可以用模拟栈或者递归来实现 这里算是比较偷懒了
代码实现
class Solution {
public:
vector<int> printListFromTailToHead(ListNode* head) {
vector<int> ans;
while(head)
{
ans.push_back(head->val);
head=head->next;
}
return vector<int>(ans.rbegin(),ans.rend());
}
};