JZ03 从尾到头打印链表
本文最后更新于:2022年4月9日 中午
Solution
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
|
class Solution { public: vector<int> printListFromTailToHead(ListNode* head) { vector<int> array; ListNode* cur = head; while (cur) { array.insert(array.begin(), cur->val); cur = cur->next; } return array; } };
|
| class Solution { public: vector<int> printListFromTailToHead(ListNode* head) { res.clear(); travel(head); return res; } private: vector<int> res; void travel(ListNode* head) { if (head == nullptr) return; travel(head->next); res.push_back(head->val); } };
|