# [3217\. Delete Nodes From Linked List Present in Array](https://leetcode.com/problems/delete-nodes-from-linked-list-present-in-array/) :::spoiler Hint ```cpp= /** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode() : val(0), next(nullptr) {} * ListNode(int x) : val(x), next(nullptr) {} * ListNode(int x, ListNode *next) : val(x), next(next) {} * }; */ class Solution { public: ListNode* modifiedList(vector<int>& nums, ListNode* head) { // Create an unordered set from the input vector nums for quick lookup // Create a dummy node to serve as the new list's head // Initialize a pointer to the dummy node for constructing the new list // Initialize a pointer to traverse the original list // Traverse the original list while () { // If the current node's value is not in the set, add it to the new list if () { // Create a new node with the current node's value and link it to the new list // Move the prev pointer to the newly created node } // Move to the next node in the original list ``` ::: :::spoiler Solution ```cpp= /** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode() : val(0), next(nullptr) {} * ListNode(int x) : val(x), next(nullptr) {} * ListNode(int x, ListNode *next) : val(x), next(next) {} * }; */ class Solution { public: ListNode* modifiedList(vector<int>& nums, ListNode* head) { unordered_set<int> st(nums.begin(), nums.end()); ListNode* dummy = new ListNode(); ListNode* prev = dummy; ListNode* cur = head; while (cur) { if (!st.count(cur->val)) { prev->next = new ListNode(cur->val); prev = prev->next; } cur = cur->next; } return dummy->next; } }; ``` - 時間複雜度:$O(m + n)$ - 空間複雜度:$O(n)$ :::