/** * struct ListNode { * int val; * struct ListNode *next; * ListNode(int x) : * val(x), next(NULL) { * } * }; */ class Solution { public: vector<int> printListFromTailToHead(ListNode* head) { list<int> intl; while (head != nullptr){ intl.push_front(head->val); head = head->next; } vector<int> intv(intl.begin(), intl.end()); return intv; } };
C++ <list>, <vector>,和 array 的区别:
- <list>:push_front & push_back, dynamic size
- <vector>:only push_back, dynamic size
- array:fixed size
// Convert <list> l to <vector> v: vector<int> intv(l.begin(), l.end()); // Convert <list> l to array arr: int arr[l.size()]; std::copy(l.begin(), l.end(), arr);