一、题目描述
输入一个链表,从尾到头打印链表每个节点的值。
二、代码(来自牛客网Invoker_em)
用递归的思想
/**
* struct ListNode {
* int val;
* struct ListNode *next;
* ListNode(int x) :
* val(x), next(NULL) {
* }
* };
*/
class Solution {
public:
vector<int> value;
vector<int> printListFromTailToHead(ListNode* head)
{
ListNode *p=NULL;
p=head;
if(p!=NULL){
if(p->next!=NULL)
{
printListFromTailToHead(p->next);
}
value.push_back(p->val);
}
return value;
}
};