剑指 Offer 06 从尾到头打印链表
概述
https://leetcode-cn.com/problems/cong-wei-dao-tou-da-yin-lian-biao-lcof/
直接法
这里要注意直接在向量的前部插入性能比较差。
class Solution {
public:
vector<int> reversePrint(ListNode* head) {
vector<int> res;
while (head != nullptr) {
res.push_back(head->val);
head = head->next;
}
reverse(res.begin(), res.end());
return res;
}
};
Links: sword-offer-06