-
Notifications
You must be signed in to change notification settings - Fork 0
/
203.移除链表元素.cpp
65 lines (56 loc) · 1.31 KB
/
203.移除链表元素.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
#include<iostream>
/*
* @lc app=leetcode.cn id=203 lang=cpp
*
* [203] 移除链表元素
*
* https://leetcode-cn.com/problems/remove-linked-list-elements/description/
*
* algorithms
* Easy (44.32%)
* Likes: 358
* Dislikes: 0
* Total Accepted: 67.1K
* Total Submissions: 150K
* Testcase Example: '[1,2,6,3,4,5,6]\n6'
*
* 删除链表中等于给定值 val 的所有节点。
*
* 示例:
*
* 输入: 1->2->6->3->4->5->6, val = 6
* 输出: 1->2->3->4->5
*
*
*/
// @lc code=start
/* struct ListNode {
int val;
ListNode *next;
ListNode(int x) : val(x), next(NULL) {}
}; */
class Solution {
public:
ListNode* removeElements(ListNode* head, int val) {
ListNode *first = new ListNode(0);
first->next = head;
ListNode *prev = first;
ListNode *todelete = nullptr;
ListNode *cur = head;
while(cur){
if(cur->val == val){
todelete = cur;
prev->next = cur->next;
}
else
prev = cur;
cur = cur->next;
if(todelete){
delete todelete;
todelete = nullptr;
}
}
return first->next;
}
};
// @lc code=end