Skip to content

Commit

Permalink
add: 从链表中移除节点
Browse files Browse the repository at this point in the history
  • Loading branch information
yi-ge committed Jan 3, 2024
1 parent f19af60 commit 14569cc
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 1 deletion.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -417,6 +417,10 @@ C++标准库提供的数据结构实在是太多了,参考[C++标准库头文

###

- [从链表中移除节点](src/stack/remove_nodes_from_linked_list.cpp) [栈, 递归, 链表, 单调栈]

- LeetCode 2487. 从链表中移除节点 <https://leetcode.cn/problems/remove-nodes-from-linked-list>

- [两数相加 II](src/stack/add_two_numbers_ii.cpp) [栈, 链表, 数学]

- LeetCode 445. 两数相加 II <https://leetcode.cn/problems/add-two-numbers-ii>
Expand Down
Binary file added images/stack/remove_nodes_from_linked_list.jpeg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
30 changes: 30 additions & 0 deletions src/stack/remove_nodes_from_linked_list.cpp
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;
}
}
};
2 changes: 1 addition & 1 deletion test/lib/lib_test.cpp
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>

Expand Down
36 changes: 36 additions & 0 deletions test/stack/remove_nodes_from_linked_list_test.cpp
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);
}

0 comments on commit 14569cc

Please sign in to comment.