Linked List Components LeetCode solution in Python
1# Runtime: 171 ms (Top 62.36%) | Memory: 19.1 MB (Top 64.07%)2class Solution:3def numComponents(self, head: Optional[ListNode], nums: List[int]) -> int:4d, count = {}, 05for num in nums:6d[num] = 078while head:9if head.val in d:10head = head.next11while head and head.val in d:12head = head.next13count += 114else:15head = head.next16return count