标签:subject offer ext 一个 替换空格 cto 方法 bsp while
/** * struct ListNode { * int val; * struct ListNode *next; * ListNode(int x) : * val(x), next(NULL) { * } * }; */ class Solution { public: vector<int> printListFromTailToHead(ListNode* head) { vector<int> result; while(head != NULL) { result.insert(result.begin(),head->val); head = head->next; } return result; } };
方法二:使用堆
class Solution { public: vector<int> printListFromTailToHead(ListNode* head) { vector <int> result; stack<int> arr; ListNode* p=head; while(p!=NULL) { arr.push(p->val); p=p->next; } int len=arr.size(); for(int i=0;i<len;i++) { result.push_back(arr.top()); arr.pop(); } return result; } };
方法三:两个vector,倒着取元素
class Solution { public: vector<int> printListFromTailToHead(ListNode* head) { vector<int> array; vector<int> array2; ListNode* p; p=head; while(p!=NULL) { array.push_back(p->val); p=p->next; } for(int i=0;i<array.size();i++) { array2.push_back(array[array.size()-i-1]); } return array2; } };
标签:subject offer ext 一个 替换空格 cto 方法 bsp while
原文地址:https://www.cnblogs.com/orenn/p/9898573.html