/**
* 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* modifiedList(vector<int>& nums, ListNode* head)
{
unordered_set<int> st(nums.begin(), nums.end());
ListNode* dummy = new ListNode();
ListNode* prev = dummy;
ListNode* cur = head;
while (cur)
{
if (!st.count(cur->val))
{
prev->next = new ListNode(cur->val);
prev = prev->next;
}
cur = cur->next;
}
return dummy->next;
}
};
- T: O(M+N)
- S: O(N)