-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathday-111.cpp
More file actions
34 lines (26 loc) · 786 Bytes
/
day-111.cpp
File metadata and controls
34 lines (26 loc) · 786 Bytes
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
/*
Remove Linked List Elements
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
*/
// Simple solution manipulating linked list in place O(N) & O(1)
class Solution {
public:
ListNode* removeElements(ListNode* head, int val) {
while (head != NULL && head->val == val) {
head = head->next;
}
ListNode* customHead = head;
ListNode* prev = NULL;
while (customHead != NULL && customHead->next != NULL) {
if (customHead->next->val == val) {
customHead->next = customHead->next->next;
} else {
customHead = customHead->next;
}
}
return head;
}
};