💢☠💫Easiest👾Faster✅💯 Lesser🧠 🎯 C++✅Python3🐍✅Java✅C✅Python🐍✅C#✅💥🔥💫Explained☠💥🔥 Beats 💯
Intuition\n\n\n\n\n\n\n\n Describe your first thoughts on how to solve this problem. \nJavaScript []\n/**\n * Definition for singly-linked list.\n * function Li
# Intuition\n![0ehh83fsnh811.jpg](https://assets.leetcode.com/users/images/2ced942b-d24e-44f8-ab9a-313e62d1928f_1720941985.3970723.jpeg)\n![Screenshot 2024-07-14 125444.png](https://assets.leetcode.com/users/images/b7c7e5d0-4af1-43b3-b608-b2a673e2f513_1720942005.1845236.png)\n![Screenshot 2024-07-14 125452.png](https://assets.leetcode.com/users/images/d77f7d20-d92f-46d3-b70b-412772b2c0b9_1720942022.9043093.png)\n![Screenshot 2024-07-14 125459.png](https://assets.leetcode.com/users/images/97fb56f6-5d9e-4c1a-b816-0fb3d23bcabb_1720942029.8476946.png)\n![Screenshot 2024-07-14 125506.png](https://assets.leetcode.com/users/images/6ee1df69-20e0-42e5-a9bd-3da60012e4f8_1720942037.7954788.png)\n![Screenshot 2024-07-14 125513.png](https://assets.leetcode.com/users/images/1d2bc8a5-31e8-4ccc-87ac-b4030b401053_1720942045.6649325.png)\n![Screenshot 2024-07-14 125520.png](https://assets.leetcode.com/users/images/2e8feea7-c3a9-4939-b23c-ef7625ac07a1_1720942052.4774225.png)\n<!-- Describe your first thoughts on how to solve this problem. -->\n```JavaScript []\n/**\n * Definition for singly-linked list.\n * function ListNode(val, next) {\n *     this.val = (val===undefined ? 0 : val)\n *     this.next = (next===undefined ? null : next)\n * }\n */\n/**\n * @param {number[]} nums\n * @param {ListNode} head\n * @return {ListNode}\n */\nvar modifiedList = function(nums, head) {\n   const set = new Set(nums), temp = new ListNode(0, head);\n    let curr = head, prev = temp;\n    while(curr !== null){\n        if(set.has(curr.val)){\n            prev.next = curr.next;\n        }\n        else \n            prev = curr;\n        curr = curr.next;\n    }\n    return temp.next;\n};\n```\n```C++ []\n#inclde<bits/stdc++.h>\nusing namespace std;\n#define ALL(v) v.begin(). v.end()\n#define usi unordered_set<int> \n#define REVERSE(v) reverse(ALL(v))\n#define pb push_back\n#define Rep(i, n) for(int i = 0; i < n; i++)\nclass Solution {\npublic:\n    ListNode* modifiedList(vector<int>& nums, ListNode* head) {\n        usi system(ALL(nums));\n        vector<ListNode*> helper;\n        while(head){\n            if(!system.contains(head->val))\n                helper.pb(head);\n            head = head->next;\n        }\n        ListNode* prev = nullptr;\n        REVERSE(helper);\n        Rep(i, helper.size()){\n            head = new ListNode(helper[i]->val, prev);\n            prev = head;\n        }\n        return head;\n    }\n};\n```\n```python3 []\n# Definition for singly-linked list.\n# class ListNode:\n#     def __init__(self, val=0, next=None):\n#         self.val = val\n#         self.next = next\nclass Solution:\n    def modifiedList(self, nums: List[int], head: Optional[ListNode]) -> Optional[ListNode]:\n        if not head:\n            return None\n    \n        system = set(nums)\n        newhead = ListNode(0)\n        newhead.next = head\n    \n        prev = newhead\n        curr = head\n        \n        while curr:\n            if curr.val in system:\n                # Remove current node\n                prev.next = curr.next\n                curr = curr.next  # Move curr to the next node (don\'t move prev)\n            else:\n                # Move both pointers forward\n                prev = curr\n                curr = curr.next\n    \n        return newhead.next\n```\n```C# []\n/**\n * Definition for singly-linked list.\n * public class ListNode {\n *     public int val;\n *     public ListNode next;\n *     public ListNode(int val=0, ListNode next=null) {\n *         this.val = val;\n *         this.next = next;\n *     }\n * }\n */\npublic class Solution {\n    public ListNode ModifiedList(int[] nums, ListNode head) {\n        HashSet<int> set = new(nums);\n        List<ListNode> helper = new List<ListNode>();\n        while(head != null){\n            if(!set.Contains(head.val))\n                helper.Add(head);\n            head = head.next;\n        }\n        helper.Reverse();\n        ListNode prev = null;\n        for(int i = 0; i < helper.Count(); ++i){\n            head = new ListNode(helper[i].val, prev);\n            prev = head;\n        }\n        return head;\n    }\n}\n```\n```java []\n/**\n * Definition for singly-linked list.\n * public class ListNode {\n *     int val;\n *     ListNode next;\n *     ListNode() {}\n *     ListNode(int val) { this.val = val; }\n *     ListNode(int val, ListNode next) { this.val = val; this.next = next; }\n * }\n */\nclass Solution {\n    public ListNode modifiedList(int[] nums, ListNode head) {\n        HashSet<Integer> set = new HashSet<>();\n        for(int x : nums)\n            set.add(x);\n        List<ListNode> helper = new ArrayList<>();\n        while(head != null){\n            if(!set.contains(head.val))\n                helper.add(head);\n            head = head.next;\n        }\n        ListNode prev = null;\n        Collections.reverse(helper);\n        for(int i = 0; i < helper.size(); ++i){\n            head = new ListNode(helper.get(i).val, prev);\n            prev = head;\n        }\n        return head;\n    }\n}\n```\n```python []\n# Definition for singly-linked list.\n# class ListNode(object):\n#     def __init__(self, val=0, next=None):\n#         self.val = val\n#         self.next = next\nclass Solution(object):\n    def modifiedList(self, nums, head):\n        """\n        :type nums: List[int]\n        :type head: Optional[ListNode]\n        :rtype: Optional[ListNode]\n        """\n        if not head:\n            return None\n    \n        system = set(nums)\n        newhead = ListNode(0)\n        newhead.next = head\n    \n        prev = newhead\n        curr = head\n        \n        while curr:\n            if curr.val in system:\n                # Remove current node\n                prev.next = curr.next\n                curr = curr.next  # Move curr to the next node (don\'t move prev)\n            else:\n                # Move both pointers forward\n                prev = curr\n                curr = curr.next\n    \n        return newhead.next\n```\n```C []\n/**\n * Definition for singly-linked list.\n * struct ListNode {\n *     int val;\n *     struct ListNode *next;\n * };\n */\nstruct ListNode* newListNode(int val, struct ListNode* prev){\n    struct ListNode* temp = (struct ListNode*)malloc(sizeof(struct ListNode));\n    temp->val = val;\n    temp->next = prev;\n    return temp;\n}\nstruct ListNode* reverse(struct ListNode* head){\n    struct ListNode* curr = head, *prev = NULL;\n    while(curr){\n        struct ListNode* temp = curr->next;\n        curr->next = prev;\n        prev = curr;\n        curr = temp;\n    }\n    return prev;\n}\nstruct ListNode* modifiedList(int* nums, int numsSize, struct ListNode* head) {\n    bool present[100001] = {false};\n    for(int i = 0; i < numsSize; i++)\n        present[nums[i]] = true;\n    int *arr = (int*)malloc(sizeof(int) * 100001);\n    int x = 0;\n    while(head){\n        if(!present[head->val])\n            arr[x++] = head->val;\n        head = head->next;\n    }\n    struct ListNode* prev = NULL;\n    for(int i = 0; i < x; ++i){\n        head = newListNode(arr[i], prev);\n        prev = head;\n    }\n    return reverse(prev);\n}\n```\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n     O(n)\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n     O(n)\n# Code\n\n![th.jpeg](https://assets.leetcode.com/users/images/be70c70e-226c-4a09-a27e-8c2c51f2f60a_1720942352.8075817.jpeg)\n
['Array', 'Linked List', 'Stack', 'C', 'Python', 'C++', 'Java', 'Python3', 'JavaScript', 'C#']