从尾到头打印链表

C++小白抄题解ing
/** * struct ListNode { * int val; * struct ListNode *next; * ListNode(int x) : * val(x), next(NULL) { * } * }; */ class Solution { public: vector<int> printListFromTailToHead(ListNode* head) { vector<int> ret; while(head){ ret.push_back(head->val); head=head->next; } std::reverse(ret.begin(),ret.end()); return ret; } };
浙公网安备 33010602011771号