File tree 2 files changed +39
-0
lines changed
solution/0817.Linked List Components
2 files changed +39
-0
lines changed Original file line number Diff line number Diff line change @@ -101,6 +101,7 @@ Complete [solutions](https://github.com/doocs/leetcode/tree/master/solution) to
101
101
| 0184 | [ Department Highest Salary] ( https://github.com/doocs/leetcode/tree/master/solution/0184.Department%20Highest%20Salary ) | ` SQL ` |
102
102
| 0328 | [ Odd Even Linked List] ( https://github.com/doocs/leetcode/tree/master/solution/0328.Odd%20Even%20Linked%20List ) | ` Linked List ` |
103
103
| 0343 | [ Integer Break] ( https://github.com/doocs/leetcode/tree/master/solution/0343.Integer%20Break ) | ` Math ` , ` Dynamic Programming ` |
104
+ | 0817 | [ Linked List Components] ( https://github.com/doocs/leetcode/tree/master/solution/0817.Linked%20List%20Components ) | ` Linked List ` |
104
105
| 0938 | [ Range Sum of BST] ( https://github.com/doocs/leetcode/tree/master/solution/0938.Range%20Sum%20of%20BST ) | ` Binary Search Tree ` |
105
106
106
107
Original file line number Diff line number Diff line change
1
+ /**
2
+ * Definition for singly-linked list.
3
+ * public class ListNode {
4
+ * int val;
5
+ * ListNode next;
6
+ * ListNode(int x) { val = x; }
7
+ * }
8
+ */
9
+ class Solution {
10
+ public int numComponents (ListNode head , int [] G ) {
11
+ if (head == null || G == null ) {
12
+ return 0 ;
13
+ }
14
+ Set <Integer > set = new HashSet <>();
15
+ for (int val : G ) {
16
+ set .add (val );
17
+ }
18
+ int n = G .length ;
19
+ ListNode cur = head ;
20
+ int cnt = 0 ;
21
+ boolean flag = false ;
22
+ while (cur != null ) {
23
+ while (cur != null && set .contains (cur .val )) {
24
+ flag = true ;
25
+ cur = cur .next ;
26
+ }
27
+ if (flag ) {
28
+ ++cnt ;
29
+ flag = false ;
30
+ }
31
+
32
+ if (cur != null ) {
33
+ cur = cur .next ;
34
+ }
35
+ }
36
+ return cnt ;
37
+ }
38
+ }
You can’t perform that action at this time.
0 commit comments