原题链接:https://leetcode-cn.com/problems/cong-wei-dao-tou-da-yin-lian-biao-lcof/
1、递归
vector<int> reversePrint(ListNode* head) {
if (head == NULL) return {};
vector<int> ans = reversePrint(head->next);
ans.push_back(head->val);
return ans;
}
2、栈
vector<int> reversePrint(ListNode* head) {
stack<int> sk;
while (head != NULL) {
sk.push(head->val);
head = head->next;
}
vector<int> ans;
while (!sk.empty()) {
ans.push_back(sk.top());
sk.pop();
}
return ans;
}