牛客-从尾到头打印链表
#include <stdio.h>
#include <queue>
#include <stack>
#include <math.h>
#include <map>
#include <string.h>
#include <string>
#include <cstring>
#include <set>
using namespace std;
struct ListNode {
int val;
struct ListNode *next;
ListNode(int x) :
val(x), next(NULL) {
}
};
class Solution {
public:
vector<int> printListFromTailToHead(ListNode* head) {
stack<ListNode*> st1;
vector<int> res;
while (head!=NULL) {
st1.push(head);
head=head->next;
}
while (!st1.empty()) {
ListNode* node = st1.top();
st1.pop();
res.push_back(node->val);
}
return res;
}
};