From b3ea983215306e30865d02f29c78df8ff98464de Mon Sep 17 00:00:00 2001 From: Yaha Date: Sun, 21 May 2023 22:37:07 +0800 Subject: [PATCH] =?UTF-8?q?=E9=93=BE=E8=A1=A8=E5=8F=8D=E8=BD=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- include/jianzhi_offer.h | 41 ++++++++++++++++++++++---- src/jianzhi_offer/list_node.cpp | 28 ++++++++++++++++++ tests/jianzhi_offer/list_node_test.cpp | 22 ++++++++++++++ 3 files changed, 86 insertions(+), 5 deletions(-) diff --git a/include/jianzhi_offer.h b/include/jianzhi_offer.h index e05565d..0da8b9c 100644 --- a/include/jianzhi_offer.h +++ b/include/jianzhi_offer.h @@ -7,9 +7,6 @@ namespace yaha { class CQueue { public: - CQueue() { - } - void append_tail(int value); int delete_head(); @@ -20,7 +17,22 @@ private: class String { public: + /** + * 剑指 Offer 09. 用两个栈实现队列 + * + * @url https://leetcode.cn/leetbook/read/illustration-of-algorithm/5d3i87/ + * @param s + * @return + */ std::string replace_space(std::string s); + + /** + * 剑指 Offer 20. 表示数值的字符串 + * + * @url https://leetcode.cn/leetbook/read/illustration-of-algorithm/5d6vi6/ + * @param s + * @return + */ bool is_number(std::string s); }; @@ -29,9 +41,28 @@ public: int val; ListNode *next; ListNode(int x) : val(x), next(NULL) {} + + /** + * 剑指 Offer 06. 从尾到头打印链表 + * + * @url https://leetcode.cn/leetbook/read/illustration-of-algorithm/5dt66m/ + * @param head + * @return + */ std::vector reverse_print(ListNode *head); - void reverse_stack(ListNode *head, std::vector& res); - void reverse(ListNode *head, std::vector& res); + + void reverse_stack(ListNode *head, std::vector &res); + void reverse(ListNode *head, std::vector &res); + + /** + * 剑指 Offer 24. 反转链表 + * + * @url https://leetcode.cn/leetbook/read/illustration-of-algorithm/9pdjbm/ + * @param head + * @return + */ + ListNode *reverse_list(ListNode *head); + ListNode* reverse_list(ListNode* cur, ListNode* pre); }; } \ No newline at end of file diff --git a/src/jianzhi_offer/list_node.cpp b/src/jianzhi_offer/list_node.cpp index 8a3b9f5..78bb8f8 100644 --- a/src/jianzhi_offer/list_node.cpp +++ b/src/jianzhi_offer/list_node.cpp @@ -29,4 +29,32 @@ std::vector ListNode::reverse_print(ListNode *head) { return res; } +ListNode* ListNode::reverse_list(ListNode* cur, ListNode* pre) { + if (cur == nullptr) { + return pre; + } + ListNode* res = reverse_list(cur->next, cur); + cur->next = pre; + return res; +} + +ListNode *ListNode::reverse_list(ListNode *head) { + ListNode* cur = head; + ListNode* pre = nullptr; + + while (cur) { +// 链表转存 + ListNode* tmp = cur->next; +// 链表断开 + cur->next = pre; +// 将当前链表值赋值给最后一个 + pre = cur; + cur = tmp; + } + + return pre; + +// return reverse_list(head, nullptr); +} + } \ No newline at end of file diff --git a/tests/jianzhi_offer/list_node_test.cpp b/tests/jianzhi_offer/list_node_test.cpp index a05ed8c..ceef9ef 100644 --- a/tests/jianzhi_offer/list_node_test.cpp +++ b/tests/jianzhi_offer/list_node_test.cpp @@ -2,6 +2,9 @@ #include "jianzhi_offer.h" +/** + * 剑指 Offer 06. 从尾到头打印链表 + */ TEST(NodeListTest, BasicAssertions) { yaha::ListNode *node = new yaha::ListNode(1); node->next = new yaha::ListNode(3); @@ -21,4 +24,23 @@ TEST(NodeListStackTest, BasicAssertions) { ASSERT_EQ(2, res[0]); ASSERT_EQ(3, res[1]); ASSERT_EQ(1, res[2]); +} + +/** + * 剑指 Offer 24. 反转链表 + */ +TEST(ListReverseTest, BasicAssertions) { + yaha::ListNode *node = new yaha::ListNode(1); + node->next = new yaha::ListNode(2); + node->next->next = new yaha::ListNode(3); + node->next->next->next = new yaha::ListNode(4); + node->next->next->next->next = new yaha::ListNode(5); + + auto l = node->reverse_list(node); + ASSERT_EQ(5, l->val); + ASSERT_EQ(4, l->next->val); + ASSERT_EQ(3, l->next->next->val); + ASSERT_EQ(2, l->next->next->next->val); + ASSERT_EQ(1, l->next->next->next->next->val); + } \ No newline at end of file