剑指 Offer 06. 从尾到头打印链表
Ideas遍历链表,每次在vector的头部insert当前元素值。
Code C++class Solution { public: vector<int> reversePrint(ListNode* head) { vector<int> ans; while (head) { ans.insert(ans.begin(), head->val); head = head->next; } return ans; } };
剑指 Offer 06. 从尾到头打印链表
Ideas遍历链表,每次在vector的头部insert当前元素值。
Code C++class Solution { public: vector<int> reversePrint(ListNode* head) { vector<int> ans; while (head) { ans.insert(ans.begin(), head->val); head = head->next; } return ans; } };
微信扫码登录