剑指 Offer 06. 从尾到头打印链表-力扣

题目要求

剑指 Offer 06. 从尾到头打印链表-力扣_第1张图片

力扣题解

剑指 Offer 06. 从尾到头打印链表-力扣_第2张图片

代码

#include 
#include 
using namespace std;
struct ListNode {
	int val;
	ListNode *next;
	ListNode(int x) : val(x), next(NULL) {}

};


class Solution1
{
public:
	Solution1();
	~Solution1();
 	vector<int> reversePrint(ListNode* head) {
		recur(head);
		return res;
	}
private:
	vector<int> res;
	void recur(ListNode* head) {
		if (head == nullptr) return;
		recur(head->next);
		res.push_back(head->val);
	}
};

Solution1::Solution1()
{
}

Solution1::~Solution1()
{
}



int main() {
	ListNode *list = new ListNode(1);
	ListNode *list1 = new ListNode(2);
	ListNode *list3 = new ListNode(3);
	list->next = list1;
	list1->next = list3;
	Solution1 solution1;
	vector<int> res = solution1.reversePrint(list);
	for (auto it : res)
	{
		cout << it;
	}

}

你可能感兴趣的:(Leetcode,C++,C++,leetcode,从尾到头打印链表,知识分享,学习记录)