Subject requirements
Solve the problem
Code
#include <vector>
#include <iostream>
using namespace std;
struct ListNode {
int val;
ListNode *next;
ListNode(int x) : val(x), next(NULL) {
}
};
class Solution1
{
public:
Solution1();
~Solution1();
vector<int> reversePrint(ListNode* head) {
recur(head);
return res;
}
private:
vector<int> res;
void recur(ListNode* head) {
if (head == nullptr) return;
recur(head->next);
res.push_back(head->val);
}
};
Solution1::Solution1()
{
}
Solution1::~Solution1()
{
}
int main() {
ListNode *list = new ListNode(1);
ListNode *list1 = new ListNode(2);
ListNode *list3 = new ListNode(3);
list->next = list1;
list1->next = list3;
Solution1 solution1;
vector<int> res = solution1.reversePrint(list);
for (auto it : res)
{
cout << it;
}
}