-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy path203.RemoveLinkedListElements.cpp
53 lines (52 loc) · 1.18 KB
/
203.RemoveLinkedListElements.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
/*
* @lc app=leetcode id=203 lang=cpp
*
* [203] Remove Linked List Elements
*
* https://leetcode.com/problems/remove-linked-list-elements/description/
*
* algorithms
* Easy (39.18%)
* Likes: 2453
* Dislikes: 119
* Total Accepted: 436.7K
* Total Submissions: 1.1M
* Testcase Example: '[1,2,6,3,4,5,6]\n6'
*
* Remove all elements from a linked list of integers that have value val.
*
* Example:
*
*
* Input: 1->2->6->3->4->5->6, val = 6
* Output: 1->2->3->4->5
*
*
*/
// @lc code=start
/**
* 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* dummy = new ListNode();
dummy->next = head;
ListNode* curr = dummy;
while (curr && curr->next) {
while (curr->next->val == val) {
curr->next = curr->next->next;
}
curr = curr->next;
}
return dummy->next;
}
};
// @lc code=end