class Solution {
public int numComponents(ListNode head, int[] G) {
HashSet<Integer> set = new HashSet<>();
for(int x : G){
set.add(x);
}
ListNode cur = head;
int res = 0;
while(cur != null){
if(set.contains(cur.val)&&(cur.next == null || !set.contains(cur.next.val))){
res++;
}
cur = cur.next;
}
return res;
}
}