-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGrouping.java
More file actions
37 lines (28 loc) · 749 Bytes
/
Grouping.java
File metadata and controls
37 lines (28 loc) · 749 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
34
35
36
37
import java.util.*;
class Solution {
public int numComponents(ListNode head, int[] G) {
HashSet<Integer> vals = new HashSet<>();
for(int num : G){
vals.add(num);
}
ListNode temp = head;
int counter = 0;
int listRow = 0;
while(temp != null){
if(!vals.contains(temp.val)){
if(listRow > 0){
counter++;
listRow = 0;
}
}
else{
listRow++;
}
temp = temp.next;
}
if(listRow > 0){
counter++;
}
return counter;
}
}