Simple Hash table approach || 100% Beat
\n### Solution Breakdown\n\n#### Approach\n\n1. Find the Maximum Value in nums:\n   - Determine the largest value in nums. This ensures that the boolean array (
\n### **Solution Breakdown**\n\n#### **Approach**\n\n1. **Find the Maximum Value in `nums`**:\n   - Determine the largest value in `nums`. This ensures that the boolean array (`check`) only needs to handle values up to this maximum, optimizing memory usage.\n\n2. **Create a Boolean Array**:\n   - Use a boolean array (`check`) to mark the values present in `nums`. This provides constant-time lookups for whether a value should be removed.\n\n3. **Traverse the Linked List**:\n   - Iterate through the linked list.\n   - For each node, check if its value is:\n     - **Within bounds** of the `check` array.\n     - **Not marked** in the `check` array (indicating it should not be removed).\n   - If the node should not be removed, append it to the result list.\n\n4. **Return the Modified List**:\n   - Ensure the result list ends correctly by setting the `next` pointer of the last node to `null`.\n\n---\n\n### **Code Walkthrough**\n\n#### **Step 1: Initialize Dummy Node**\n```java []\nListNode ans = new ListNode(0, head);\n```\n- Use a dummy node (`ans`) pointing to the head of the list. This simplifies handling edge cases, such as removing the head node.\n\n#### **Step 2: Determine the Maximum Value in `nums`**\n```java []\nint max = -1;\nfor (int i : nums) {\n    max = max > i ? max : i;\n}\n```\n- Traverse the `nums` array to find the largest value. This ensures the boolean array (`check`) can accommodate all possible values in `nums`.\n\n#### **Step 3: Populate the Boolean Array**\n```java []\nboolean[] check = new boolean[max + 1];\nfor (int i : nums) {\n    check[i] = true;\n}\n```\n- Create a boolean array of size `max + 1` to map each value in `nums` to `true`.\n\n#### **Step 4: Traverse and Modify the List**\n```java []\nListNode tail = ans;\nwhile (head != null) {\n    if (head.val >= check.length || !check[head.val]) {\n        tail.next = head;  // Append the current node to the result list\n        tail = tail.next;  // Move the tail pointer\n    }\n    head = head.next;  // Move to the next node\n}\n```\n- Iterate through the linked list:\n  - If the node\u2019s value is greater than `max` (out of bounds for the `check` array) or is not marked in `check`, append the node to the result list.\n  - Otherwise, skip the node.\n\n#### **Step 5: Terminate the Result List**\n```java []\ntail.next = null;\n```\n- Ensure the `next` pointer of the last node is `null` to properly terminate the result list.\n\n#### **Step 6: Return the Modified List**\n```java []\nreturn ans.next;\n```\n- Return the new list starting from the node after the dummy node.\n\n---\n\n### **Example Walkthrough**\n\n#### **Example 1**\n**Input**:\n- `nums = [2, 3]`\n- `head = [1, 2, 3, 4, 5]`\n\n**Execution**:\n1. Find `max = 3`.\n2. Create `check = [false, false, true, true]` (values `2` and `3` are marked `true`).\n3. Traverse the list:\n   - Node `1`: Not in `check` \u2192 Append to result: `tail = [1]`.\n   - Node `2`: In `check` \u2192 Skip.\n   - Node `3`: In `check` \u2192 Skip.\n   - Node `4`: Not in `check` \u2192 Append to result: `tail = [1 \u2192 4]`.\n   - Node `5`: Not in `check` \u2192 Append to result: `tail = [1 \u2192 4 \u2192 5]`.\n\n**Output**: `[1 \u2192 4 \u2192 5]`.\n\n---\n\n#### **Example 2**\n**Input**:\n- `nums = [1, 4, 5]`\n- `head = [1, 4, 2, 5, 3]`\n\n**Execution**:\n1. Find `max = 5`.\n2. Create `check = [false, true, false, false, true, true]` (values `1`, `4`, and `5` are marked `true`).\n3. Traverse the list:\n   - Node `1`: In `check` \u2192 Skip.\n   - Node `4`: In `check` \u2192 Skip.\n   - Node `2`: Not in `check` \u2192 Append to result: `tail = [2]`.\n   - Node `5`: In `check` \u2192 Skip.\n   - Node `3`: Not in `check` \u2192 Append to result: `tail = [2 \u2192 3]`.\n\n**Output**: `[2 \u2192 3]`.\n\n---\n\n### **Time and Space Complexity**\n\n#### **Time Complexity**\n1. **Finding `max`**: \\( O(M) \\), where \\( M \\) is the size of `nums`.\n2. **Populating `check`**: \\( O(M) \\), iterating through `nums` to mark values.\n3. **Traversing the List**: \\( O(N) \\), where \\( N \\) is the number of nodes in the linked list.\n\n**Total**:  O(M + N) .\n\n#### **Space Complexity**\n1. **Boolean Array**: \\( O(\\text{max}) \\), where `max` is the largest value in `nums`.\n2. **Result List**: Uses the existing linked list nodes (in-place modification).\n\n**Total**:  O(max) .\n\n---\n\n### **Key Points**\n- **Efficiency**: The boolean array enables \\( O(1) \\) lookups, improving performance for large lists.\n- **Edge Case Handling**: The code handles values outside the range of `nums` by checking `head.val >= check.length`.\n- **Space Optimization**: The boolean array is only as large as the maximum value in `nums`.
['Array', 'Hash Table', 'Linked List', 'Java']