/**
* struct ListNode {* int val;
* struct ListNode *next;
* ListNode(int x) :
* val(x), next(NULL) {
* }
* };
*/
#栈
#include<vector>#include<iostream>
using namespace std;
class Solution {
public:
vector<int> printListFromTailToHead(ListNode* head) {
vector <int> result;
if(head==NULL)
return result;
stack<ListNode*> reverse;
ListNode* node=head;
while(node!=NULL)
{
reverse.push(node);
node=node->next;
}
while(!reverse.empty())
{
node=reverse.top(); #取栈顶元素
result.push_back(node->val);
reverse.pop();
}
return result;
}
};
#反转vector
class Solution {
public:
vector<int> printListFromTailToHead(ListNode* head) {
vector<int> a;
ListNode *p;
p=head;
while(p!=NULL){
a.push_back(p->val);
p=p->next;
}
reverse(a.begin(),a.end());//反转
return a;
}
};
#递归
class Solution {
public:
vector<int> dev;
vector<int>& printListFromTailToHead(struct ListNode* head) {
if(head!=NULL) {
if(head->next!=NULL) {
dev=printListFromTailToHead(head->next);
}
dev.push_back(head->val);
}
return dev;
}
};
public:
vector<int> dev;
vector<int>& printListFromTailToHead(struct ListNode* head) {
if(head!=NULL) {
if(head->next!=NULL) {
dev=printListFromTailToHead(head->next);
}
dev.push_back(head->val);
}
return dev;
}
};