Detailed Explanation, Using STACK and SET || JAVA C++ PYTHON JS
Intuition\nStore nums in a HashSet: The nums array is stored in a HashSet to allow fast lookups (O(1) average time complexity).\n\nTraverse the Linked List: The
# Intuition\nStore nums in a HashSet: The nums array is stored in a HashSet to allow fast lookups (O(1) average time complexity).\n\nTraverse the Linked List: The linked list is traversed, and nodes with values not in the HashSet are pushed onto a stack. This stack helps reverse the order of these nodes.\n\nRebuild the List: The stack is then used to pop nodes and rebuild a new linked list, effectively reversing the order of the retained nodes.\n\n\n# Complexity\n- Time complexity: O(n + m)\n\n- Space complexity: O(m + 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        HashSet<Integer> set = new HashSet<>();\n        Stack<ListNode> stack = new Stack<>();\n        ListNode current = head;\n\n        for (int i : nums) set.add(i);\n        while (current != null) {\n            if (!set.contains(current.val)) {\n                stack.push(current);\n            }\n            current = current.next;\n        }\n        ListNode newHead = null;\n        while (!stack.isEmpty()) {\n            ListNode node = stack.pop();\n            node.next = newHead;\n            newHead = node;\n        }\n        return newHead;\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 */\nvar modifiedList = function(nums, head) {\n    let set = new Set(nums);\n    let stack = [];\n    let current = head;\n\n    while (current !== null) {\n        if (!set.has(current.val)) {\n            stack.push(current);\n        }\n        current = current.next;\n    }\n\n    let newHead = null;\n    while (stack.length > 0) {\n        let node = stack.pop();\n        node.next = newHead;\n        newHead = node;\n    }\n\n    return newHead;\n};\n\n```\n```python []\n# Definition for singly-linked list.\nclass ListNode:\n    def __init__(self, val=0, next=None):\n        self.val = val\n        self.next = next\n\nclass Solution:\n    def modifiedList(self, nums, head):\n        set_nums = set(nums)\n        stack = []\n        current = head\n\n        while current:\n            if current.val not in set_nums:\n                stack.append(current)\n            current = current.next\n\n        new_head = None\n        while stack:\n            node = stack.pop()\n            node.next = new_head\n            new_head = node\n\n        return new_head\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 */\n#include <unordered_set>\n#include <stack>\n\nclass Solution {\npublic:\n    ListNode* modifiedList(vector<int>& nums, ListNode* head) {\n        unordered_set<int> set(nums.begin(), nums.end());\n        stack<ListNode*> stk;\n        ListNode* current = head;\n\n        while (current != nullptr) {\n            if (set.find(current->val) == set.end()) {\n                stk.push(current);\n            }\n            current = current->next;\n        }\n\n        ListNode* newHead = nullptr;\n        while (!stk.empty()) {\n            ListNode* node = stk.top();\n            stk.pop();\n            node->next = newHead;\n            newHead = node;\n        }\n\n        return newHead;\n    }\n};\n\n```\n\n# Code\n
['Linked List', 'Ordered Set', 'Python', 'C++', 'Java', 'JavaScript']