题目
书店店员有一张链表形式的书单,每个节点代表一本书,节点中的值表示书的编号。为更方便整理书架,店员需要将书单倒过来排列,就可以从最后一本书开始整理,逐一将书放回到书架上。请倒序返回这个书单链表。
输入
head = [3,6,4,1]
输出
[1,4,6,3]
解法 1:递归
| class Solution { |
| public: |
| std::vector<int> reverseBookList(ListNode* head) { |
| recur(head); |
| return res; |
| } |
| private: |
| std::vector<int> res; |
| void recur(ListNode* head) { |
| if (head == nullptr) return; |
| recur(head->next); |
| res.push_back(head->val); |
| } |
| }; |
解法 2:辅助栈法
| class Solution { |
| public: |
| std::vector<int> getResultList(ListNode* head) { |
| std::stack<int> stk; |
| std::vector<int> res; |
| while(head!=nullptr) { |
| stk.push(head->val); |
| head = head->next; |
| } |
| while(!stk.empty()) { |
| res.push_back(stk.top()); |
| stk.pop(); |
| } |
| return res; |
| } |
| }; |
完整代码
| #include <iostream> |
| #include <vector> |
| struct ListNode { |
| int val; |
| ListNode* next; |
| ListNode(int x) : val(x), next(NULL) {} |
| }; |
| |
| class Solution { |
| public: |
| std::vector<int> reverseBookList(ListNode* head) { |
| recur(head); |
| return res; |
| } |
| private: |
| std::vector<int> res; |
| void recur(ListNode* head) { |
| if (head == nullptr) return; |
| recur(head->next); |
| res.push_back(head->val); |
| } |
| }; |
| |
| void test() { |
| ListNode* head = new ListNode(1); |
| ListNode* n2 = new ListNode(2); |
| ListNode* n3 = new ListNode(3); |
| ListNode* n4 = new ListNode(4); |
| ListNode* n5 = new ListNode(5); |
| |
| head->next = n2; |
| n2->next = n3; |
| n3->next = n4; |
| n4->next = n5; |
| |
| Solution solution; |
| std::vector<int> reversedList = solution.reverseBookList(head); |
| |
| std::cout << "Result: " << std::endl; |
| for (int num : reversedList) { |
| std::cout << num << " "; |
| } |
| std::cout << std::endl; |
| |
| } |
| |
| int main() { |
| test(); |
| return 0; |
| } |
| #include <iostream> |
| #include <vector> |
| #include <stack> |
| struct ListNode { |
| int val; |
| ListNode* next; |
| ListNode(int x) : val(x), next(NULL) {} |
| }; |
| |
| class Solution { |
| public: |
| std::vector<int> getResultList(ListNode* head) { |
| std::stack<int> stk; |
| std::vector<int> res; |
| while(head!=nullptr) { |
| stk.push(head->val); |
| head = head->next; |
| } |
| while(!stk.empty()) { |
| res.push_back(stk.top()); |
| stk.pop(); |
| } |
| return res; |
| } |
| }; |
| |
| void test() { |
| ListNode* head = new ListNode(1); |
| ListNode* n2 = new ListNode(2); |
| ListNode* n3 = new ListNode(3); |
| ListNode* n4 = new ListNode(4); |
| ListNode* n5 = new ListNode(5); |
| |
| head->next = n2; |
| n2->next = n3; |
| n3->next = n4; |
| n4->next = n5; |
| |
| Solution solution; |
| std::vector<int> testList = solution.getResultList(head); |
| |
| std::cout << "Result: " << std::endl; |
| for (int x : testList) { |
| std::cout << x << " "; |
| } |
| std::cout << std::endl; |
| } |
| int main() { |
| test(); |
| return 0; |
| } |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 单元测试从入门到精通
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 上周热点回顾(3.3-3.9)
· winform 绘制太阳,地球,月球 运作规律