[Beats 100%🔥] Counting | Easy and Beginner Friendly Solution 🚀
IntuitionWe need to remove nodes from a linked list based on the values present in an array. Using a direct lookup mechanism will make deletions efficient.Appro
# Intuition
We need to remove nodes from a linked list based on the values present in an array. Using a direct lookup mechanism will make deletions efficient.

# Approach
1. **Find the maximum value** in the `nums` array to create a boolean lookup table.
2. **Mark elements in a frequency array** for quick existence checks.
3. **Traverse the linked list and remove nodes**:
   - If the node's value exists in the `nums` array, remove it.
   - Adjust the head if necessary.
   - Maintain proper links when skipping nodes.

# Complexity
- **Time complexity:** $$O(n + m)$$  
  - `O(m)` to process `nums`, where `m` is the array length.  
  - `O(n)` to traverse the linked list.  
- **Space complexity:** $$O(max)$$  
  - A boolean array of size `max+1` for quick lookup.


# Code
```java []
class Solution {
    public ListNode modifiedList(int[] nums, ListNode head) {
        ListNode prev = null, pres = head, next = head != null ? head.next : null;

        // Find the max value in nums array
        int max = -1;
        for (int num : nums) {
            max = Math.max(max, num);
        }

        // Create a boolean lookup table
        boolean[] freq = new boolean[max + 1];
        for (int num : nums) freq[num] = true;

        // Traverse the linked list and remove nodes
        while (pres != null) {
            if (pres.val < freq.length && freq[pres.val]) { // If value exists in nums
                if (pres == head) {
                    head = head.next; // Update head if first node is removed
                } else {
                    prev.next = next; // Remove the node
                }
                //move to next node
                pres = next;
                if (next != null) next = next.next;
            } else {
                //move to next node
                prev = pres;
                pres = next;
                if (next != null) next = next.next;
            }
        }
        return head;
    }
}

```
**Please Upvote⬆️**
![Screenshot 2025-02-15 134751.png](https://assets.leetcode.com/users/images/652f2f2d-5420-4293-af1b-30691ba3e534_1739873739.2343125.png)

['Array', 'Linked List', 'Counting', 'Java']