#include <iostream>
#include <vector>
#include <stack>
struct ListNode {
int val;
ListNode *next;
ListNode(int x) : val(x), next(nullptr) {}
};
std::vector<int> reversePrint(ListNode* head) {
std::stack<int> s;
std::vector<int> result;
ListNode* current = head;
while (current != nullptr) {
s.push(current->val);
current = current->next;
}
while (!s.empty()) {
result.push_back(s.top());
s.pop();
}
return result;
}
int main() {
ListNode* head = new ListNode(2);
head->next = new ListNode(3);
head->next->next = new ListNode(5);
std::vector<int> output = reversePrint(head);
std::cout << "返回结果: [";
for (size_t i = 0; i < output.size(); ++i) {
std::cout << output[i];
if (i < output.size() - 1) {
std::cout << ", ";
}
}
std::cout << "]" << std::endl;
delete head->next->next;
delete head->next;
delete head;
return 0;
}