Leetcode 分享筆記

817. Linked List Components

題目

leetcode
b

python


        count = 0
        hasConnection = False
        while head:
            if head.val in nums:
                hasConnection = True
            elif hasConnection:
                count += 1
                hasConnection = False
            head = head.next
        if hasConnection:
            count += 1
        return count