😎 Best Solution ||  💯 % on Runtime 🚀 ||  💻 With Explanation
\n\n\n### Intuition  \nImagine you\'re running a party, but only some guests are invited (based on a list of numbers). If a guest shows up and isn\'t on the lis
![Screenshot (2985).png](https://assets.leetcode.com/users/images/26768bc9-3716-4f24-8b6c-85b4b808d04a_1725609031.9055815.png)\n\n\n### Intuition  \nImagine you\'re running a party, but only some guests are invited (based on a list of numbers). If a guest shows up and isn\'t on the list, you need to remove them. That\'s what we\'re doing here with linked lists. We want to keep only the nodes whose values match the numbers from the array.\n\n### Approach  \n1. First, we find the largest number in the list to know how big our frequency array needs to be.\n2. We create a frequency array (like a guest list) where `True` means the number is invited, and `False` means they aren\'t.\n3. Then we walk through the linked list. If a node\u2019s value is on the guest list (i.e., the frequency array says `True`), we skip it. If not, we add it to our new, modified linked list.\n4. Finally, we return the new linked list that only contains the uninvited guests (nodes with values not in the array).\n\n### Time Complexity  \n- Time complexity:  \n  We first loop through the array to find the max value and then build a frequency array based on that. After that, we loop through both the `nums` array and the linked list. So, overall it\'s $$O(n + m)$$ where `n` is the length of `nums` and `m` is the length of the linked list.\n\n### Space Complexity  \n- Space complexity:  \n  We need extra space for the frequency array, which depends on the largest number in the input array. Therefore, the space complexity is $$O(k)$$, where `k` is the largest number in `nums`.\n\n---\n\n# Code\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        int max = -1;\n        for(int num : nums ){\n            max = num > max ? num : max;\n        }\n        boolean[] freq = new boolean[max+1];\n\n        for(int num : nums) freq[num] = true;\n\n        ListNode temp = new ListNode();\n        ListNode current = temp;\n\n        while(head != null){\n            if( head.val >= freq.length || freq[head.val] == false){\n                current.next = head;\n                current = current.next;\n            }\n            head = head.next;\n        }\n\n        current.next = null;\n        return temp.next;\n    }\n}\n```\n``` C++ []\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        int max = -1;\n        for (int num : nums) {\n            max = num > max ? num : max;\n        }\n\n        vector<bool> freq(max + 1, false);\n\n        for (int num : nums) {\n            freq[num] = true;\n        }\n\n        ListNode* temp = new ListNode();\n        ListNode* current = temp;\n\n        while (head != nullptr) {\n            if (head->val >= freq.size() || !freq[head->val]) {\n                current->next = head;\n                current = current->next;\n            }\n            head = head->next;\n        }\n\n        current->next = nullptr;\n\n        return temp->next;\n    }\n};\n```\n\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    let max = -1;\n    for( let num of nums ){\n        max = num > max ? num : max;\n    }\n\n    let freq = new Array(max+1).fill(false);\n\n    for(let num of nums)freq[num] = true;\n\n    let temp = new ListNode();\n    let current = temp;\n\n    while(head != null ){\n        if( head.val >= freq.length || freq[head.val] == false){\n            current.next = head;\n            current = current.next;\n        }\n        head = head.next;\n    }\n\n    current.next = null;\n    return temp.next;\n};\n\n\n```\n\n``` Python []\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        max_val = -1\n        for num in nums:\n            max_val = max(num, max_val)\n\n        freq = [False] * (max_val + 1)\n\n        for num in nums:\n            freq[num] = True\n\n        temp = ListNode()\n        current = temp\n\n        while head:\n            if head.val >= len(freq) or not freq[head.val]:\n                current.next = head\n                current = current.next\n            head = head.next\n\n        current.next = None\n\n        return temp.next\n\n        \n```\n\nHappy Coding \uD83D\uDCBB\n\n![upvote image.jpg](https://assets.leetcode.com/users/images/05bb3b98-3026-44b2-a529-d04512a88ca9_1725609131.0824568.jpeg)\n
['Array', 'Linked List', 'Counting', 'C++', 'Java', 'Python3', 'JavaScript']