思路:
这道题很简单,可以用dfs来做,也可以翻转链表后来做,具体看代码即可。
代码:
/**
* 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;
}
};