-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSolution.java
More file actions
33 lines (27 loc) · 760 Bytes
/
Solution.java
File metadata and controls
33 lines (27 loc) · 760 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
/**
@lc id : 817
@problem : Linked List Components
@author : github.com/rohitkumar-rk
@url : https://leetcode.com/problems/linked-list-components/
@difficulty : medium
@tags : Linked List
*/
class Solution {
public int numComponents(ListNode head, int[] G){
HashSet<Integer> set = new HashSet<Integer>();
for(int g : G){
set.add(g);
}
int components = 0;
while(head != null){
if(set.contains(head.val)){
components++;
head = head.next;
while(head != null && set.contains(head.val))
head = head.next;
}
else head = head.next;
}
return components;
}
}