Skip to content

Commit 2ec0c47

Browse files
committed
feat(solution): add solution 0817 [Java]
Linked List Components
1 parent 19d5da1 commit 2ec0c47

File tree

2 files changed

+39
-0
lines changed

2 files changed

+39
-0
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,7 @@ Complete [solutions](https://github.com/doocs/leetcode/tree/master/solution) to
101101
| 0184 | [Department Highest Salary](https://github.com/doocs/leetcode/tree/master/solution/0184.Department%20Highest%20Salary) | `SQL` |
102102
| 0328 | [Odd Even Linked List](https://github.com/doocs/leetcode/tree/master/solution/0328.Odd%20Even%20Linked%20List) | `Linked List` |
103103
| 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` |
104105
| 0938 | [Range Sum of BST](https://github.com/doocs/leetcode/tree/master/solution/0938.Range%20Sum%20of%20BST) | `Binary Search Tree` |
105106

106107

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
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+
}

0 commit comments

Comments
 (0)