Linked List Components LeetCode solution in Java

1
class Solution {
2
public int numComponents(ListNode head, int[] nums) {
3
int count = 0;
4
HashSet<Integer> set = new HashSet();
5
for (int i = 0; i < nums.length; i++) {
6
set.add(nums[i]);
7
}
8
while (head != null) {
9
if (set.contains(head.val)) {
10
while (head.next != null && set.contains(head.next.val)) {
11
head = head.next;
12
}
13
count++;
14
}
15

16
head = head.next;
17
}
18
return count;
19
}
20
}

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0