Dcc-: 06/09/2024 || LinkedList || O(N)🔥
Approach:\nThe problem seems to involve removing nodes from a linked list where the node\'s value is present in a given array nums. Here\'s the approach and int
### Approach:\nThe problem seems to involve removing nodes from a linked list where the node\'s value is present in a given array `nums`. Here\'s the approach and intuition behind the solution:\n\n1. **Use of a HashSet**: \n   - The array `nums` is first converted into a `HashSet`. This is done because checking for membership in a `HashSet` is faster (constant time on average, O(1)) than checking in a list (O(n) in the worst case).\n   \n2. **Removing Nodes from the Start**:\n   - The solution first ensures that any nodes at the beginning of the linked list (`head`) that have values present in the `HashSet` are removed. This loop progresses until it finds the first node whose value is not in the `HashSet`.\n\n3. **Traversing the Remaining List**:\n   - A `temp` pointer is used to traverse the list, and a `prev` pointer keeps track of the last node that remains in the list. For every node:\n     - If the current node\'s value (`temp.val`) is in the `HashSet`, the node is removed by updating `prev.next` to `temp.next`.\n     - If the current node\u2019s value is not in the `HashSet`, `prev` is updated to be the current node (`temp`).\n\n4. **Return the Modified List**: \n   - After completing the traversal and removal of nodes, the modified list is returned.\n\n### Time and Space Complexity:\n\n#### Time Complexity:\n1. **HashSet Construction**: Constructing the `HashSet` from the array `nums` takes O(m) time, where `m` is the length of the array.\n2. **First While Loop (Removing from the start)**: The first while loop potentially runs through the first few nodes until it finds a node that isn\u2019t in the `HashSet`. In the worst case, this will iterate over all `n` nodes in the list.\n3. **Second While Loop (Removing from the rest)**: The second loop iterates through the remaining nodes, and for each node, checking membership in the `HashSet` takes O(1). Hence, this loop also takes O(n) in total.\n\nThus, the total time complexity is **O(m + n)**, where `m` is the size of the array `nums` and `n` is the number of nodes in the linked list.\n\n#### Space Complexity:\n1. The `HashSet` takes **O(m)** space, where `m` is the size of the input array `nums`.\n2. No additional space is required for the linked list itself, so the overall space complexity is **O(m)**.\n\n### Intuition:\nThe intuition behind this approach is to make use of the faster lookup times provided by a `HashSet` to efficiently remove nodes with values present in the `nums` array. By updating pointers in the linked list as needed, we avoid creating a new list, keeping the space usage optimal while maintaining a linear time traversal.\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        \n        HashSet<Integer> list = new HashSet<>();\n        \n        for(int i:nums)\n            list.add(i);\n        \n        while(head!=null && list.contains(head.val)){\n            \n            head = head.next;\n        }\n        \n        ListNode temp = head;\n        ListNode prev = head;\n        \n        while(temp!=null){\n            \n            if(list.contains(temp.val)){\n                prev.next = temp.next;\n                temp = temp.next;\n            }\n            else{\n                \n                prev = temp;\n            temp = temp.next;\n                \n            }\n            \n         \n        }\n        return head;\n        \n    }\n}\n```
['Linked List', 'Java']