{1,2,3}
[3,2,1]
{67,0,24,58}
[58,24,0,67]
/** * struct ListNode { * int val; * struct ListNode *next; * ListNode(int x) : * val(x), next(NULL) { * } * }; */ class Solution { public: vector<int> printListFromTailToHead(struct ListNode* head) { vector<int> value; if(head != NULL) { value.insert(value.begin(),head->val); if(head->next != NULL) { vector<int> tempVec = printListFromTailToHead(head->next); if(tempVec.size()>0) value.insert(value.begin(),tempVec.begin(),tempVec.end()); } } return value; } };
/** * struct ListNode { * int val; * struct ListNode *next; * ListNode(int x) : * val(x), next(NULL) { * } * }; */ class Solution { public: vector<int> printListFromTailToHead(struct ListNode* head) { vector<int> value; if(head != NULL) { value.insert(value.begin(),head->val); while(head->next != NULL) { value.insert(value.begin(),head->next->val); head = head->next; } } return value; } };
这道题你会答吗?花几分钟告诉大家答案吧!
扫描二维码,关注牛客网
下载牛客APP,随时随地刷题