Idea: Esta
pregunta es muy simple, puede usar dfs para hacerlo, o puede voltear la lista vinculada y hacerlo más tarde, solo mire el código.
Código:
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
void dfs(vector<int>& ans,ListNode* head){
if(head==NULL) return ;
dfs(ans,head->next);
ans.push_back(head->val);
return ;
}
vector<int> reversePrint(ListNode* head) {
vector<int> ans;
dfs(ans,head);
return ans;
}
};