1371 · 链表组件
public int numComponents(ListNode head, int[] G) {
Set<Integer> set = new HashSet<>();
for (int x : G) set.add(x);
int res = 0;
while (head != null) {
if (set.contains(head.val) && (head.next == null || !set.contains(head.next.val))) res++;
head = head.next;
}
return res;
}