C++ | EASY TO UNDERSTAND | DETAILED SOLUTION
Intuition\n Describe your first thoughts on how to solve this problem. \nThe problem requires modifying a linked list by removing all nodes that have values pre
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nThe problem requires modifying a linked list by removing all nodes that have values present in a given vector. My first thought is to use a set for efficient lookup of values that need to be removed. By iterating through the linked list and checking each node\'s value against the set, we can determine whether to keep or remove each node.\n# Approach\n<!-- Describe your approach to solving the problem. -->\nInitialization:\n\nCreate a set from the given vector nums for quick lookups.\nUse a dummy node to simplify edge cases where the head of the list might be removed.\nIterate through the linked list:\n\nTraverse the list with a pointer curr, starting from the dummy node.\nFor each node, check if the value is in the set.\nIf the value is in the set, bypass the node by adjusting the next pointer of the previous node.\nIf the value is not in the set, move the pointer curr to the next node.\nReturn the modified list:\n\nReturn the list starting from dummy->next, which skips the dummy node.\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\no(n)\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\no(n)\n# Code\n```\n/**\n * Definition for singly-linked list.\n * struct ListNode {\n *     int val;\n *     ListNode *next;\n *     ListNode() : val(0), next(nullptr) {}\n *     ListNode(int x) : val(x), next(nullptr) {}\n *     ListNode(int x, ListNode *next) : val(x), next(next) {}\n * };\n */\nclass Solution {\npublic:\n    ListNode* modifiedList(vector<int>& nums, ListNode* head) {\n        \n        set<int> st;\n        \n        for(auto v:nums)\n        {\n            st.insert(v);\n        }\n        \n        ListNode* dummy = new ListNode(-1,head);\n        ListNode* newhead = dummy;\n        ListNode* curr = newhead;\n        \n        while(curr->next!=NULL)\n        {\n            if(st.find(curr->next->val) != st.end())\n            {\n                curr->next = curr->next->next;\n                \n            }\n            else{\n            curr = curr->next;\n            }\n        }\n        \n        return dummy->next;\n    }\n};\n```
['Python', 'C++', 'Java']