Skip to content

Commit 81d8863

Browse files
authored
Create 3217. Delete Nodes From Linked List Present in Array 1 (#923)
2 parents 98b86ca + be5b2ba commit 81d8863

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
class Solution {
2+
public:
3+
ListNode* modifiedList(vector<int>& nums, ListNode* head) {
4+
unordered_set<int> s(nums.begin(), nums.end());
5+
vector<int> vec;
6+
ListNode* curr = head;
7+
8+
while (curr) {
9+
if (s.count(curr->val) == 0) vec.push_back(curr->val);
10+
curr = curr->next;
11+
}
12+
13+
if (vec.empty()) return nullptr;
14+
15+
curr = head;
16+
ListNode* prev = nullptr;
17+
for (int v : vec) {
18+
curr->val = v;
19+
prev = curr;
20+
curr = curr->next;
21+
}
22+
23+
if (prev) prev->next = nullptr;
24+
return head;
25+
}
26+
};

0 commit comments

Comments
 (0)