题目: 输入一个链表,从尾到头打印链表每个节点的值。
思路:递归(栈)
代码:class Solution {
public:
vector<int> res;
vector<int>& printListFromTailToHead(ListNode* head) {
//day 3 周一 2017-6-12日
// 递归解决(栈)
// 声明个存储的vector
vector<int> res;
// ListNode *p =head;
//while(p!=NULL)
if(head!=NULL)
{
//不为空,往下走
if(head->next!=NULL) res=printListFromTailToHead(head->next);
//打印
res.push_back(head->val);
}
// return res;
return res;
}
};