> ## 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.

# Linked List Components Python Solution

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

LeetCode 817, [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/linked-list-components/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 817   # by problem number
lcpy gen -s linked_list_components   # by problem name
```

## Problem

You are given the `head` of a linked list containing unique integer values and an integer array `nums` that is a subset of the linked list values.

Return the number of **connected components** in `nums`. A connected component is a non-empty, maximal sequence of **consecutive** nodes in the linked list such that every node's value belongs to `nums`.

### Examples

![Example 1](https://assets.leetcode.com/uploads/2021/07/22/lc-linkedlistcom1.jpg)

```
Input: head = [0,1,2,3], nums = [0,1,3]
Output: 2
Explanation: 0 and 1 are connected, so [0, 1] and [3] are the two connected components.
```

![Example 2](https://assets.leetcode.com/uploads/2021/07/22/lc-linkedlistcom2.jpg)

```
Input: head = [0,1,2,3,4], nums = [0,3,1,4]
Output: 2
Explanation: 0 and 1 are connected, 3 and 4 are connected, so [0, 1] and [3, 4] are the two connected components.
```

### Constraints

* The number of nodes in the linked list is n.
* 1 \<= n \<= 10\<sup>4\</sup>
* 0 \<= Node.val \< n
* All the values Node.val are unique.
* 1 \<= nums.length \<= n
* 0 \<= nums\[i] \< n
* All the values of nums are unique.

## Solution

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

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


class Solution:
    # Time: O(n)
    # Space: O(m) for the nums set
    def num_components(self, head: ListNode[int] | None, nums: list[int]) -> int:
        values = set(nums)
        count = 0
        in_component = False
        node = head
        while node is not None:
            if node.val in values:
                if not in_component:
                    count += 1
                    in_component = True
            else:
                in_component = False
            node = node.next
        return count
```

## Complexity

| Time | Space |
| - | - |
| O(n) | O(m) for the nums set |

## Tags


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