-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
71 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
// 从链表中移除节点 | ||
// https://leetcode.cn/problems/remove-nodes-from-linked-list | ||
// INLINE ../../images/stack/remove_nodes_from_linked_list.jpeg | ||
|
||
#include <headers.hpp> | ||
|
||
/** | ||
* Definition for singly-linked list. | ||
* struct ListNode { | ||
* int val; | ||
* ListNode *next; | ||
* ListNode() : val(0), next(nullptr) {} | ||
* ListNode(int x) : val(x), next(nullptr) {} | ||
* ListNode(int x, ListNode *next) : val(x), next(next) {} | ||
* }; | ||
*/ | ||
class Solution { | ||
public: | ||
ListNode *removeNodes(ListNode *head) { | ||
if (head == nullptr) { | ||
return nullptr; | ||
} | ||
head->next = removeNodes(head->next); | ||
if (head->next != nullptr && head->val < head->next->val) { | ||
return head->next; | ||
} else { | ||
return head; | ||
} | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
// 执行编译时间:2024-01-02 09:21:49 | ||
// 执行编译时间:2024-01-03 10:37:56 | ||
#include <gtest/gtest.h> | ||
#include <lib.hpp> | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
#include <stack/remove_nodes_from_linked_list.cpp> | ||
|
||
TEST(从链表中移除节点, ListNode) { | ||
Solution solution; | ||
// 示例 1: | ||
// 输入:head = [5,2,13,3,8] | ||
// 输出:[13,8] | ||
// 解释:需要移除的节点是 5 ,2 和 3 。 | ||
// - 节点 13 在节点 5 右侧。 | ||
// - 节点 13 在节点 2 右侧。 | ||
// - 节点 8 在节点 3 右侧。 | ||
ListNode *head = new ListNode(5); | ||
head->next = new ListNode(2); | ||
head->next->next = new ListNode(13); | ||
head->next->next->next = new ListNode(3); | ||
head->next->next->next->next = new ListNode(8); | ||
ListNode *result = solution.removeNodes(head); | ||
EXPECT_EQ(result->val, 13); | ||
EXPECT_EQ(result->next->val, 8); | ||
EXPECT_EQ(result->next->next, nullptr); | ||
|
||
// 示例 2: | ||
// 输入:head = [1,1,1,1] | ||
// 输出:[1,1,1,1] | ||
// 解释:每个节点的值都是 1 ,所以没有需要移除的节点。 | ||
head = new ListNode(1); | ||
head->next = new ListNode(1); | ||
head->next->next = new ListNode(1); | ||
head->next->next->next = new ListNode(1); | ||
result = solution.removeNodes(head); | ||
EXPECT_EQ(result->val, 1); | ||
EXPECT_EQ(result->next->val, 1); | ||
EXPECT_EQ(result->next->next->val, 1); | ||
EXPECT_EQ(result->next->next->next->val, 1); | ||
EXPECT_EQ(result->next->next->next->next, nullptr); | ||
} |