Simple Java || C++ Code ☠️
Code (Using sorting) :\n\njava []\n/**\n * Definition for singly-linked list.\n * public class ListNode {\n *     int val;\n *     ListNode next;\n *     ListNo
### Code (Using sorting) :\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        int n = nums.length;\n        Arrays.sort(nums);\n        ListNode slow = head;\n        ListNode fast = slow.next;\n        while(fast!=null){\n            if(fast.val>nums[n-1]){\n                slow=fast;\n                fast=fast.next;\n                continue;\n            }\n            if(help(nums,fast.val)){\n                slow.next=fast.next;\n                fast=fast.next;\n            }\n            else{\n                slow=fast;\n                fast=fast.next;\n            }\n        }\n        for(int i=0;i<n;i++){\n            if(head.val==nums[i]){\n                head = head.next;\n            }\n        }\n        return head;\n    }\n    public boolean help(int[] nums,int a){\n        int l=0,h=nums.length-1,mid=0;\n        while(l<=h){\n            mid=(l+h)/2;\n            if(nums[mid]==a){\n                return true;\n            }\n            else if(nums[mid]>a){\n                h=mid-1;\n            }\n            else{\n                l=mid+1;\n            }\n        }\n        return false;\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 n = nums.size();\n        sort(nums.begin(),nums.end());\n        ListNode* slow = head;\n        ListNode* fast = slow->next;\n        while(fast!=NULL){\n            if(fast->val>nums[n-1]){\n                slow=fast;\n                fast=fast->next;\n                continue;\n            }\n            if(help(nums,fast->val)){\n                slow->next=fast->next;\n                fast=fast->next;\n            }\n            else{\n                slow=fast;\n                fast=fast->next;\n            }\n        }\n        for(int i=0;i<n;i++){\n            if(head->val==nums[i]){\n                head = head->next;\n            }\n        }\n        return head;\n    }\n    bool help(vector<int>& nums,int a){\n        int l=0,h=nums.size()-1,mid=0;\n        while(l<=h){\n            mid=(l+h)/2;\n            if(nums[mid]==a){\n                return true;\n            }\n            else if(nums[mid]>a){\n                h=mid-1;\n            }\n            else{\n                l=mid+1;\n            }\n        }\n        return false;\n    }\n};\n```\n\n### Code (Using HashSet) :\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 n = nums.length;\n        HashSet<Integer> set = new HashSet<>();\n        for(int i=0;i<n;i++){\n            set.add(nums[i]);\n        }\n        while(set.contains(head.val)){\n            head=head.next;\n        }\n        ListNode slow = head;\n        ListNode fast = slow.next;\n        while(fast!=null){\n            if(set.contains(fast.val)){\n                slow.next = fast.next;\n                fast=fast.next;\n            }\n            else{\n                slow=fast;\n                fast=fast.next;\n            }\n        }\n        return head;\n    }\n}\n```\n\n\n### Explanation For First Code:\n\n\n\n\n1. **Initialization**:\n   - The array `nums` is sorted to enable binary search.\n   - Two pointers, `slow` and `fast`, are initialized to traverse the linked list.\n\n2. **Traversal and Removal**:\n   - Traverse the linked list using `fast` pointer.\n   - If `fast.val` is greater than the maximum value in `nums`, move both `slow` and `fast` pointers forward.\n   - If `fast.val` is found in `nums` using the `help` method (binary search), remove the `fast` node by updating `slow.next`.\n   - If `fast.val` is not in `nums`, move both pointers forward.\n\n3. **Handling the Head Node**:\n   - After the main traversal, check if the head node\'s value is in `nums` and remove it if necessary.\n\n4. **Return the Modified List**:\n   - Return the updated head of the linked list.\n\n### Helper Method (Binary Search)\n\nThe `help` method performs a binary search to check if a value `a` exists in the sorted array `nums`.\n\n\n\n\n### Example\n\nSuppose we have the following inputs:\n- `nums = [2, 3, 5]`\n- Linked list: `1 -> 2 -> 3 -> 4 -> 5 -> 6`\n\n### Step-by-Step Execution\n\n1. **Initial Setup**:\n   - Sort the `nums` array: `nums = [2, 3, 5]` (already sorted in this case).\n   - Initialize `slow` to point to the head of the list (`1`).\n   - Initialize `fast` to point to the second node (`2`).\n\n2. **First Iteration**:\n   - `fast.val = 2`, which is in `nums`.\n   - Remove the node with value `2` by setting `slow.next` to `fast.next`:\n     - Linked list becomes: `1 -> 3 -> 4 -> 5 -> 6`\n   - Move `fast` to the next node (`3`).\n\n3. **Second Iteration**:\n   - `fast.val = 3`, which is in `nums`.\n   - Remove the node with value `3` by setting `slow.next` to `fast.next`:\n     - Linked list becomes: `1 -> 4 -> 5 -> 6`\n   - Move `fast` to the next node (`4`).\n\n4. **Third Iteration**:\n   - `fast.val = 4`, which is not in `nums`.\n   - Move both `slow` and `fast` pointers forward:\n     - `slow` points to `4`, `fast` points to `5`.\n\n5. **Fourth Iteration**:\n   - `fast.val = 5`, which is in `nums`.\n   - Remove the node with value `5` by setting `slow.next` to `fast.next`:\n     - Linked list becomes: `1 -> 4 -> 6`\n   - Move `fast` to the next node (`6`).\n\n6. **Fifth Iteration**:\n   - `fast.val = 6`, which is not in `nums`.\n   - Move both `slow` and `fast` pointers forward:\n     - `slow` points to `6`, `fast` is now `null`.\n\n7. **End of Main Traversal**:\n   - The main traversal ends since `fast` is `null`.\n   - Now, we need to check if the head node (`1`) is in `nums`:\n     - It is not, so we keep the head as it is.\n\n### Final Result\nThe modified linked list is:\n- `1 -> 4 -> 6`\n\n### Detailed Code Execution\n\nLet\'s break down the code execution for this example:\n\n1. **Initialization**:\n   ```java\n   int n = nums.length; // n = 3\n   Arrays.sort(nums); // nums = [2, 3, 5]\n   ListNode slow = head; // slow points to node with value 1\n   ListNode fast = slow.next; // fast points to node with value 2\n   ```\n\n2. **First Iteration**:\n   ```java\n   if (help(nums, fast.val)) { // help(nums, 2) returns true\n       slow.next = fast.next; // remove node with value 2\n       fast = fast.next; // fast moves to node with value 3\n   } else {\n       slow = fast;\n       fast = fast.next;\n   }\n   ```\n\n3. **Second Iteration**:\n   ```java\n   if (help(nums, fast.val)) { // help(nums, 3) returns true\n       slow.next = fast.next; // remove node with value 3\n       fast = fast.next; // fast moves to node with value 4\n   } else {\n       slow = fast;\n       fast = fast.next;\n   }\n   ```\n\n4. **Third Iteration**:\n   ```java\n   if (help(nums, fast.val)) { // help(nums, 4) returns false\n       slow = fast;\n       fast = fast.next; // slow points to node with value 4, fast moves to node with value 5\n   } else {\n       slow = fast;\n       fast = fast.next;\n   }\n   ```\n\n5. **Fourth Iteration**:\n   ```java\n   if (help(nums, fast.val)) { // help(nums, 5) returns true\n       slow.next = fast.next; // remove node with value 5\n       fast = fast.next; // fast moves to node with value 6\n   } else {\n       slow = fast;\n       fast = fast.next;\n   }\n   ```\n\n6. **Fifth Iteration**:\n   ```java\n   if (help(nums, fast.val)) { // help(nums, 6) returns false\n       slow = fast;\n       fast = fast.next; // slow points to node with value 6, fast is now null\n   } else {\n       slow = fast;\n       fast = fast.next;\n   }\n   ```\n\n7. **Final Check**:\n   ```java\n   for (int i = 0; i < n; i++) {\n       if (head.val == nums[i]) { // head.val = 1, nums does not contain 1\n           head = head.next; // no change\n       }\n   }\n   ```\n\nThe final linked list is `1 -> 4 -> 6`, which is returned.\n\n\n\n\n\n### Complexity Analysis\n\n- **Time Complexity**:\n  - Sorting the `nums` array takes \\(O(n log n)\\).\n  - Traversing the linked list and performing binary search for each node takes \\(O(m \\log n)\\), where \\(m\\) is the number of nodes in the linked list.\n  - Overall time complexity: \\(O(n log n + m log n)\\).\n\n- **Space Complexity**:\n  - The space complexity is \\(O(1)\\) if the space for the output and input is not counted. Otherwise, the space complexity is \\(O(n + m)\\) for storing the `nums` array and the linked list.\n\n### Explanation of second code:\n\n\n\n1. **Convert the array to a HashSet**: The code first converts the array `nums` into a `HashSet`. This allows for O(1) average time complexity when checking whether a value exists in the set.\n\n   ```java\n   HashSet<Integer> set = new HashSet<>();\n   for(int i=0;i<n;i++){\n       set.add(nums[i]);\n   }\n   ```\n\n2. **Remove nodes from the head**: If the value of the current head node is in the `set`, the code updates the head to the next node, effectively removing the current node.\n\n   ```java\n   while(set.contains(head.val)){\n       head = head.next;\n   }\n   ```\n\n3. **Traverse the list and remove nodes**: After ensuring the new head has a value not in the set, the code uses two pointers, `slow` and `fast`. The `slow` pointer always points to the last node that shouldn\'t be removed, and the `fast` pointer checks the next nodes. When a node with a value in `set` is found, it is skipped by updating `slow.next`.\n\n   ```java\n   ListNode slow = head;\n   ListNode fast = slow.next;\n   while(fast != null){\n       if(set.contains(fast.val)){\n           slow.next = fast.next;  // Skip the node\n           fast = fast.next;       // Move fast pointer ahead\n       }\n       else{\n           slow = fast;            // Move slow pointer ahead\n           fast = fast.next;       // Move fast pointer ahead\n       }\n   }\n   ```\n\n4. **Return the modified list**: After removing all nodes whose values exist in `nums`, the modified linked list starting from `head` is returned.\n\n### Example:\n#### Input:\n- `nums = [1, 3, 5]`\n- `linked list: 1 -> 2 -> 3 -> 4 -> 5`\n\n#### Process:\n1. Convert `nums` into a set: `{1, 3, 5}`.\n2. Start at the head:\n   - Remove node with value `1` (head becomes `2`).\n   - Traverse to node `3` (remove it).\n   - Traverse to node `5` (remove it).\n\n#### Output:\n- Modified linked list: `2 -> 4`\n\n### Time Complexity:\n- **O(n)** to create the `HashSet` from the array `nums`, where `n` is the length of the array.\n- **O(m)** to traverse the linked list and remove nodes, where `m` is the number of nodes in the list.\n\nSo, the overall time complexity is **O(n + m)**.\n\n
['Array', 'Linked List', 'Simulation', 'C++', 'Java']