标签:subject public res pre desc result 使用 bsp oid
/** * 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; if(head == nullptr){ return result; } stack<int> s; ListNode* pHead = head; while(pHead != nullptr){ s.push(pHead -> val); pHead = pHead -> next; } int sz = s.size(); for(int i = 0;i < sz;++i){ result.push_back(s.top()); s.pop(); } return result; } };
2、递归
class Solution { public: void helper(ListNode* head,vector<int> &result){ if(head == nullptr){ return; } helper(head -> next,result); result.push_back(head -> val);//一定要把位置放在这里 } vector<int> printListFromTailToHead(ListNode* head) { vector<int> result; if(head == nullptr){ return result; } helper(head,result); return result; } };
标签:subject public res pre desc result 使用 bsp oid
原文地址:http://www.cnblogs.com/dingxiaoqiang/p/7881341.html