> ## Documentation Index
> Fetch the complete documentation index at: https://leetcode-py.wisl.dev/llms.txt
> Use this file to discover all available pages before exploring further.

> ## Agent Instructions
> leetcode-py is a Python LeetCode practice environment generator with one CLI: lcpy. It is not a service or platform.
> Each problem is a directory under leetcode/ with README.md, solution.py, test_solution.py, helpers.py, and playground.ipynb. lcpy gen creates them from JSON templates bundled with the package.
> Examples are backed by tests; copy them verbatim.

# Delete Nodes From Linked List Present in Array

> Tested Python solution for LeetCode 3217 with 16 pytest cases. Generate a practice environment with lcpy.

LeetCode 3217, [Medium](/catalog/medium). Topics: [Array](/catalog/topics/array), [Hash Table](/catalog/topics/hash-table), [Linked List](/catalog/topics/linked-list). [View on LeetCode](https://leetcode.com/problems/delete-nodes-from-linked-list-present-in-array/description/).

Generate this problem as a practice environment: tested reference solution, 16 [parametrized pytest cases](/practice/testing), and a playground notebook:

```bash theme={"theme":{"light":"github-light","dark":"github-dark"}}
lcpy gen -n 3217   # by problem number
lcpy gen -s delete_nodes_from_linked_list_present_in_array   # by problem name
```

## Problem

You are given an array of integers `nums` and the `head` of a linked list. Return the `head` of the modified linked list after \<strong>removing\</strong> all nodes from the linked list that have a value that exists in `nums`.

### Examples

![Example 1](https://assets.leetcode.com/uploads/2024/06/11/linkedlistexample0.png)

```
Input: nums = [1,2,3], head = [1,2,3,4,5]
Output: [4,5]
Explanation: Remove the nodes with values 1, 2, and 3.
```

![Example 2](https://assets.leetcode.com/uploads/2024/06/11/linkedlistexample1.png)

```
Input: nums = [1], head = [1,2,1,2,1,2]
Output: [2,2,2]
Explanation: Remove the nodes with value 1.
```

![Example 3](https://assets.leetcode.com/uploads/2024/06/11/linkedlistexample2.png)

```
Input: nums = [5], head = [1,2,3,4]
Output: [1,2,3,4]
Explanation: No node has value 5.
```

### Constraints

* 1 \<= nums.length \<= 10\<sup>5\</sup>
* 1 \<= nums\[i] \<= 10\<sup>5\</sup>
* All elements in nums are unique.
* The number of nodes in the given list is in the range \[1, 10\<sup>5\</sup>].
* 1 \<= Node.val \<= 10\<sup>5\</sup>
* The input is generated such that there is at least one node in the linked list that has a value not present in nums.

## Solution

Reference implementation from [solution.py on GitHub](https://github.com/wislertt/leetcode-py/blob/main/leetcode/delete_nodes_from_linked_list_present_in_array/solution.py), full suite in [test\_solution.py](https://github.com/wislertt/leetcode-py/blob/main/leetcode/delete_nodes_from_linked_list_present_in_array/test_solution.py):

```python theme={"theme":{"light":"github-light","dark":"github-dark"}}
from leetcode_py import ListNode


class Solution:
    # Time: O(n + m) where n = len(nums), m = list length
    # Space: O(n) for the value set
    def modified_list(self, nums: list[int], head: ListNode[int] | None) -> ListNode[int] | None:
        drop = set(nums)
        dummy: ListNode[int] = ListNode(0)
        tail = dummy
        node = head
        while node is not None:
            nxt = node.next
            if node.val not in drop:
                tail.next = node
                tail = node
                node.next = None
            node = nxt
        return dummy.next
```

## Complexity

| Time | Space |
| - | - |
| O(n + m) where n = len(nums), m = list length | O(n) for the value set |

## Tags

[NeetCode All](/catalog/neetcode).


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.