题解 | 从尾到头打印链表
/** * struct ListNode { * int val; * struct ListNode *next; * ListNode(int x) : * val(x), next(NULL) { * } * }; */ class Solution { public: vector<int> ret = vector<int>(); vector<int> printListFromTailToHead(ListNode* head) { //timeout // while(head->next != NULL) // { // int current = head->val; // ret.insert(ret.begin(),current); // } // return ret; if (head != NULL) { printListFromTailToHead(head->next); ret.push_back(head->val); } return ret; } };
通过recursive解决问题