Skip to content

Commit 14569cc

Browse files
committed
add: 从链表中移除节点
1 parent f19af60 commit 14569cc

File tree

5 files changed

+71
-1
lines changed

5 files changed

+71
-1
lines changed

README.md

+4
Original file line numberDiff line numberDiff line change
@@ -417,6 +417,10 @@ C++标准库提供的数据结构实在是太多了,参考[C++标准库头文
417417

418418
###
419419

420+
- [从链表中移除节点](src/stack/remove_nodes_from_linked_list.cpp) [栈, 递归, 链表, 单调栈]
421+
422+
- LeetCode 2487. 从链表中移除节点 <https://leetcode.cn/problems/remove-nodes-from-linked-list>
423+
420424
- [两数相加 II](src/stack/add_two_numbers_ii.cpp) [栈, 链表, 数学]
421425

422426
- LeetCode 445. 两数相加 II <https://leetcode.cn/problems/add-two-numbers-ii>
74.9 KB
Loading
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
// 从链表中移除节点
2+
// https://leetcode.cn/problems/remove-nodes-from-linked-list
3+
// INLINE ../../images/stack/remove_nodes_from_linked_list.jpeg
4+
5+
#include <headers.hpp>
6+
7+
/**
8+
* Definition for singly-linked list.
9+
* struct ListNode {
10+
* int val;
11+
* ListNode *next;
12+
* ListNode() : val(0), next(nullptr) {}
13+
* ListNode(int x) : val(x), next(nullptr) {}
14+
* ListNode(int x, ListNode *next) : val(x), next(next) {}
15+
* };
16+
*/
17+
class Solution {
18+
public:
19+
ListNode *removeNodes(ListNode *head) {
20+
if (head == nullptr) {
21+
return nullptr;
22+
}
23+
head->next = removeNodes(head->next);
24+
if (head->next != nullptr && head->val < head->next->val) {
25+
return head->next;
26+
} else {
27+
return head;
28+
}
29+
}
30+
};

test/lib/lib_test.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// 执行编译时间:2024-01-02 09:21:49
1+
// 执行编译时间:2024-01-03 10:37:56
22
#include <gtest/gtest.h>
33
#include <lib.hpp>
44

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
#include <stack/remove_nodes_from_linked_list.cpp>
2+
3+
TEST(从链表中移除节点, ListNode) {
4+
Solution solution;
5+
// 示例 1:
6+
// 输入:head = [5,2,13,3,8]
7+
// 输出:[13,8]
8+
// 解释:需要移除的节点是 5 ,2 和 3 。
9+
// - 节点 13 在节点 5 右侧。
10+
// - 节点 13 在节点 2 右侧。
11+
// - 节点 8 在节点 3 右侧。
12+
ListNode *head = new ListNode(5);
13+
head->next = new ListNode(2);
14+
head->next->next = new ListNode(13);
15+
head->next->next->next = new ListNode(3);
16+
head->next->next->next->next = new ListNode(8);
17+
ListNode *result = solution.removeNodes(head);
18+
EXPECT_EQ(result->val, 13);
19+
EXPECT_EQ(result->next->val, 8);
20+
EXPECT_EQ(result->next->next, nullptr);
21+
22+
// 示例 2:
23+
// 输入:head = [1,1,1,1]
24+
// 输出:[1,1,1,1]
25+
// 解释:每个节点的值都是 1 ,所以没有需要移除的节点。
26+
head = new ListNode(1);
27+
head->next = new ListNode(1);
28+
head->next->next = new ListNode(1);
29+
head->next->next->next = new ListNode(1);
30+
result = solution.removeNodes(head);
31+
EXPECT_EQ(result->val, 1);
32+
EXPECT_EQ(result->next->val, 1);
33+
EXPECT_EQ(result->next->next->val, 1);
34+
EXPECT_EQ(result->next->next->next->val, 1);
35+
EXPECT_EQ(result->next->next->next->next, nullptr);
36+
}

0 commit comments

Comments
 (0)