将一个链表反向输入
- 定义一个vector,递归在最后一个节点插入
- 或者用栈,先进后出,插入vector
- 注意 vector的使用,递归时&
#include <vector> //可以不用加头文件
//vector俩种插入方式:pash_back
//insert(a.end(), val); 不用初始化容量大小 【】要
//面试题 宁可用递归也不用栈
class Solution {
public:
vector<int> printListFromTailToHead(ListNode* head) {
vector<int> arrList;
if (head == NULL)
return arrList;
_print(arrList, head);
return arrList;
}
void _print(vector<int>& arrList, ListNode* head)
{
if (head->next != NULL)
{
_print(arrList, head->next);
}
arrList.push_back(head->val);
//arrList.insert(arrList.end(), head->val);
}
};