-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path203-remove-linked-list-elements.cpp
81 lines (76 loc) · 1.69 KB
/
203-remove-linked-list-elements.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
//给你一个链表的头节点 head 和一个整数 val ,请你删除链表中所有满足 Node.val == val 的节点,并返回 新的头节点 。
//
//
//
// 示例 1:
//
//
//输入:head = [1,2,6,3,4,5,6], val = 6
//输出:[1,2,3,4,5]
//
//
// 示例 2:
//
//
//输入:head = [], val = 1
//输出:[]
//
//
// 示例 3:
//
//
//输入:head = [7,7,7,7], val = 7
//输出:[]
//
//
//
//
// 提示:
//
//
// 列表中的节点数目在范围 [0, 10⁴] 内
// 1 <= Node.val <= 50
// 0 <= val <= 50
//
//
// Related Topics 递归 链表 👍 1365 👎 0
#include "headers.h"
//leetcode submit region begin(Prohibit modification and deletion)
/**
* 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 *removeElements(ListNode *head, int val) {
ListNode *dummyHead = new ListNode();
dummyHead->next = head;
auto *cur = dummyHead;
while (cur->next != nullptr) {
if (cur->next->val == val) {
auto *tmp = cur->next;
cur->next = cur->next->next;
delete tmp;
} else {
cur = cur->next;
}
}
head = dummyHead->next;
delete dummyHead;
return head;
}
};
//leetcode submit region end(Prohibit modification and deletion)
int main() {
Solution s;
vector<int> arr{1, 2, 6, 3, 4, 5, 6};
auto *head = new ListNode(arr);
auto res = s.removeElements(head, 6);
showListNode(res);
}