Linked List Components LeetCode solution in Python

1
# Runtime: 171 ms (Top 62.36%) | Memory: 19.1 MB (Top 64.07%)
2
class Solution:
3
def numComponents(self, head: Optional[ListNode], nums: List[int]) -> int:
4
d, count = {}, 0
5
for num in nums:
6
d[num] = 0
7

8
while head:
9
if head.val in d:
10
head = head.next
11
while head and head.val in d:
12
head = head.next
13
count += 1
14
else:
15
head = head.next
16
return count

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0