Skip to content

Latest commit

 

History

History
38 lines (28 loc) · 656 Bytes

817.md

File metadata and controls

38 lines (28 loc) · 656 Bytes

Linked List Components

Description

link


Solution

  • See Code

Code

O(n)

# Definition for singly-linked list.
# class ListNode:
#     def __init__(self, x):
#         self.val = x
#         self.next = None

class Solution:
    def numComponents(self, head: ListNode, G: List[int]) -> int:
        res = 0
        G = set(G)
        while head:
            if head.val in G:
                res += 1
                while head and head.val in G:
                    head = head.next
            else:
                head = head.next
        return res