简单递归
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
vector<int> reversePrint(ListNode* head) {
vector<int> ans;
reverseLink(ans,head);
return ans;
}
// 递归函数
void reverseLink(vector<int>& ans,ListNode* head){
if(head==nullptr) return;
reverseLink(ans,head->next);
ans.push_back(head->val);
}
};
时间、空间复杂度都是.