Skip to content

Commit 63d9528

Browse files
authored
Create 3217. Delete Nodes From Linked List Present in Array (#577)
2 parents a75a805 + 346cfa8 commit 63d9528

File tree

1 file changed

+40
-0
lines changed

1 file changed

+40
-0
lines changed
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/**
2+
* Definition for singly-linked list.
3+
* struct ListNode {
4+
* int val;
5+
* ListNode *next;
6+
* ListNode() : val(0), next(nullptr) {}
7+
* ListNode(int x) : val(x), next(nullptr) {}
8+
* ListNode(int x, ListNode *next) : val(x), next(next) {}
9+
* };
10+
*/
11+
class Solution {
12+
public:
13+
ListNode* modifiedList(vector<int>& nums, ListNode* head) {
14+
15+
unordered_set<int> s(nums.begin(), nums.end());
16+
17+
ListNode* headd = NULL;
18+
ListNode* tail = NULL;
19+
ListNode* curr = head;
20+
21+
while(curr)
22+
{
23+
if(!s.count(curr->val))
24+
{
25+
ListNode* Node = new ListNode(curr->val);
26+
27+
if(headd == NULL)
28+
headd = tail = Node;
29+
else
30+
{
31+
tail->next = Node;
32+
tail = Node;
33+
}
34+
}
35+
curr = curr->next;
36+
}
37+
tail->next = NULL;
38+
return headd;
39+
}
40+
};

0 commit comments

Comments
 (0)