class Solution {
public:
vector<int> printListFromTailToHead(ListNode* head) {
/*方法1
stack<ListNode*> node;
vector<int> arr;
ListNode* p=head;
while(p!=NULL)
{
node.push(p);
p=p->next;
}
while(!node.empty())
{
arr.push_back((node.top())->val);
node.pop();
}
if(head)
{
if(head->next==NULL)
{
arr.push_back(head->val);
return arr;
}
else
{
arr=printListFromTailToHead(head->next);
arr.push_back(head->val);
}
}
return arr;
}
};
public:
vector<int> printListFromTailToHead(ListNode* head) {
/*方法1
stack<ListNode*> node;
vector<int> arr;
ListNode* p=head;
while(p!=NULL)
{
node.push(p);
p=p->next;
}
while(!node.empty())
{
arr.push_back((node.top())->val);
node.pop();
}
*/
//方法2
vector<int> arr;if(head)
{
if(head->next==NULL)
{
arr.push_back(head->val);
return arr;
}
else
{
arr=printListFromTailToHead(head->next);
arr.push_back(head->val);
}
}
return arr;
}
};