Simple and Easy C++ Solution | 💻 With Explanation
Intuition\n Describe your first thoughts on how to solve this problem. \nSince we will nedd to go node by node and check if this is the one to be removed or not
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nSince we will nedd to go node by node and check if this is the one to be removed or not, I took unordered_set to store all the elements of array in set , so that i can perform st.find(node->val), that is, if it exists in the set or not.\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n1) Connecting nodes that do not exist in the set, and maintaining head correctly. If head is found in the set, then move it forward, and delete the previous head you just remove using delete(), so that node is not left hanging.\n\n\n2) Once we have settled with head, now you can move on to other nodes, by keeping curr to the present node you are standing on, checking before hand if it\'s front position exists or not? If not, then connect curr to node that comes after it\'s next.\n1->2->3\nSuppose 2 exists in the set, so then remove 1\'s connection from 2 and joint it with 3.\n\n3) if curr and curr->next both are not in the set, then you can just move curr forwad.\n\n4) Hope ,it helps.\n\n# Complexity\n- Time complexity: O(N+M)\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity: O(N)\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```cpp []\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        unordered_set<int>st(begin(nums), end(nums));     // inserted\n\n        while(head!=NULL && st.find(head->val)!=st.end())\n        {\n            //we should delete this hanging node also\n            ListNode* temp = head;\n            head = head->next;\n            delete(temp);\n        }\n\n        ListNode* curr = head;\n        while(curr!=NULL && curr->next!=NULL)\n        {\n            if(st.find(curr->next->val)!=st.end())    // pehle hi dekh lenge ki agar iska aage wala exist ni karta toh hata denge isko , or uske aage wale ke join kar denge curr ko \n            {\n                ListNode* temp = curr->next;\n                curr->next = curr->next->next;\n                delete(temp);\n            }\n            else\n            {\n                curr = curr->next;\n            }\n        }\n        return head;\n    }\n};\n```
['Array', 'Hash Table', 'Linked List', 'C++']