Linked List Components

1
2
3
4
5
6
7
8
9
class Solution:
    def numComponents(self, head: Optional[ListNode], nums: List[int]) -> int:
        numbers = set(nums)
        result = 0
        while head:
            if head.val in numbers and (head.next == None or head.next.val not in numbers):
                result += 1
            head = head.next
        return result