Skip to content

Commit 888ba06

Browse files
committed
feat: add solutions to lc problem: No.0817
No.0817.Linked List Components
1 parent 3a8df2b commit 888ba06

File tree

7 files changed

+322
-74
lines changed

7 files changed

+322
-74
lines changed

solution/0800-0899/0817.Linked List Components/README.md

Lines changed: 115 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -48,14 +48,13 @@
4848

4949
<!-- 这里可写通用的实现逻辑 -->
5050

51-
定义 pre 表示是否可加 1,初始为 true。
51+
**方法一:哈希表 + 链表一次遍历**
5252

53-
遍历链表各个结点:
53+
题目中需要判断链表中节点的值是否在数组 `nums` 中,因此我们可以使用哈希表 $s$ 存储数组 `nums` 中的值。
5454

55-
- 若当前结点值在 nums 中,并且当前为可加 1 的状态,则 `res++`,并且将 pre 状态置为 false;
56-
- 若当前结点值不在 nums 中,则将 pre 置为 true,表示可加 1。
55+
然后遍历链表,找到第一个在哈希表 $s$ 中的节点,然后从该节点开始遍历,直到遇到不在哈希表 $s$ 中的节点,这样就找到了一个组件,然后继续遍历链表,直到遍历完整个链表。
5756

58-
最后返回 res 即可
57+
时间复杂度 $O(n)$,空间复杂度 $O(n)$。其中 $n$ 为链表的节点个数
5958

6059
<!-- tabs:start -->
6160

@@ -70,18 +69,16 @@
7069
# self.val = val
7170
# self.next = next
7271
class Solution:
73-
def numComponents(self, head: ListNode, nums: List[int]) -> int:
72+
def numComponents(self, head: Optional[ListNode], nums: List[int]) -> int:
73+
ans = 0
7474
s = set(nums)
75-
res, pre = 0, True
7675
while head:
77-
if head.val in s:
78-
if pre:
79-
res += 1
80-
pre = False
81-
else:
82-
pre = True
83-
head = head.next
84-
return res
76+
while head and head.val not in s:
77+
head = head.next
78+
ans += head is not None
79+
while head and head.val in s:
80+
head = head.next
81+
return ans
8582
```
8683

8784
### **Java**
@@ -101,28 +98,118 @@ class Solution:
10198
*/
10299
class Solution {
103100
public int numComponents(ListNode head, int[] nums) {
101+
int ans = 0;
104102
Set<Integer> s = new HashSet<>();
105-
for (int num : nums) {
106-
s.add(num);
103+
for (int v : nums) {
104+
s.add(v);
107105
}
108-
int res = 0;
109-
boolean pre = true;
110106
while (head != null) {
111-
if (s.contains(head.val)) {
112-
if (pre) {
113-
++res;
114-
pre = false;
115-
}
116-
} else {
117-
pre = true;
107+
while (head != null && !s.contains(head.val)) {
108+
head = head.next;
109+
}
110+
ans += head != null ? 1 : 0;
111+
while (head != null && s.contains(head.val)) {
112+
head = head.next;
118113
}
119-
head = head.next;
120114
}
121-
return res;
115+
return ans;
122116
}
123117
}
124118
```
125119

120+
### **C++**
121+
122+
```cpp
123+
/**
124+
* Definition for singly-linked list.
125+
* struct ListNode {
126+
* int val;
127+
* ListNode *next;
128+
* ListNode() : val(0), next(nullptr) {}
129+
* ListNode(int x) : val(x), next(nullptr) {}
130+
* ListNode(int x, ListNode *next) : val(x), next(next) {}
131+
* };
132+
*/
133+
class Solution {
134+
public:
135+
int numComponents(ListNode* head, vector<int>& nums) {
136+
unordered_set<int> s(nums.begin(), nums.end());
137+
int ans = 0;
138+
while (head) {
139+
while (head && !s.count(head->val)) head = head->next;
140+
ans += head != nullptr;
141+
while (head && s.count(head->val)) head = head->next;
142+
}
143+
return ans;
144+
}
145+
};
146+
```
147+
148+
### **Go**
149+
150+
```go
151+
/**
152+
* Definition for singly-linked list.
153+
* type ListNode struct {
154+
* Val int
155+
* Next *ListNode
156+
* }
157+
*/
158+
func numComponents(head *ListNode, nums []int) int {
159+
s := map[int]bool{}
160+
for _, v := range nums {
161+
s[v] = true
162+
}
163+
ans := 0
164+
for head != nil {
165+
for head != nil && !s[head.Val] {
166+
head = head.Next
167+
}
168+
if head != nil {
169+
ans++
170+
}
171+
for head != nil && s[head.Val] {
172+
head = head.Next
173+
}
174+
}
175+
return ans
176+
}
177+
```
178+
179+
### **JavaScript**
180+
181+
```js
182+
/**
183+
* Definition for singly-linked list.
184+
* function ListNode(val, next) {
185+
* this.val = (val===undefined ? 0 : val)
186+
* this.next = (next===undefined ? null : next)
187+
* }
188+
*/
189+
/**
190+
* @param {ListNode} head
191+
* @param {number[]} nums
192+
* @return {number}
193+
*/
194+
var numComponents = function (head, nums) {
195+
const s = new Set();
196+
for (const v of nums) {
197+
s.add(v);
198+
}
199+
let ans = 0;
200+
while (head) {
201+
while (head && !s.has(head.val)) {
202+
head = head.next;
203+
}
204+
ans += head != null;
205+
while (head && s.has(head.val)) {
206+
head = head.next;
207+
}
208+
}
209+
return ans;
210+
};
211+
```
212+
126213
### **...**
127214

128215
```

solution/0800-0899/0817.Linked List Components/README_EN.md

Lines changed: 111 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -51,18 +51,16 @@
5151
# self.val = val
5252
# self.next = next
5353
class Solution:
54-
def numComponents(self, head: ListNode, nums: List[int]) -> int:
54+
def numComponents(self, head: Optional[ListNode], nums: List[int]) -> int:
55+
ans = 0
5556
s = set(nums)
56-
res, pre = 0, True
5757
while head:
58-
if head.val in s:
59-
if pre:
60-
res += 1
61-
pre = False
62-
else:
63-
pre = True
64-
head = head.next
65-
return res
58+
while head and head.val not in s:
59+
head = head.next
60+
ans += head is not None
61+
while head and head.val in s:
62+
head = head.next
63+
return ans
6664
```
6765

6866
### **Java**
@@ -80,28 +78,118 @@ class Solution:
8078
*/
8179
class Solution {
8280
public int numComponents(ListNode head, int[] nums) {
81+
int ans = 0;
8382
Set<Integer> s = new HashSet<>();
84-
for (int num : nums) {
85-
s.add(num);
83+
for (int v : nums) {
84+
s.add(v);
8685
}
87-
int res = 0;
88-
boolean pre = true;
8986
while (head != null) {
90-
if (s.contains(head.val)) {
91-
if (pre) {
92-
++res;
93-
pre = false;
94-
}
95-
} else {
96-
pre = true;
87+
while (head != null && !s.contains(head.val)) {
88+
head = head.next;
89+
}
90+
ans += head != null ? 1 : 0;
91+
while (head != null && s.contains(head.val)) {
92+
head = head.next;
9793
}
98-
head = head.next;
9994
}
100-
return res;
95+
return ans;
10196
}
10297
}
10398
```
10499

100+
### **C++**
101+
102+
```cpp
103+
/**
104+
* Definition for singly-linked list.
105+
* struct ListNode {
106+
* int val;
107+
* ListNode *next;
108+
* ListNode() : val(0), next(nullptr) {}
109+
* ListNode(int x) : val(x), next(nullptr) {}
110+
* ListNode(int x, ListNode *next) : val(x), next(next) {}
111+
* };
112+
*/
113+
class Solution {
114+
public:
115+
int numComponents(ListNode* head, vector<int>& nums) {
116+
unordered_set<int> s(nums.begin(), nums.end());
117+
int ans = 0;
118+
while (head) {
119+
while (head && !s.count(head->val)) head = head->next;
120+
ans += head != nullptr;
121+
while (head && s.count(head->val)) head = head->next;
122+
}
123+
return ans;
124+
}
125+
};
126+
```
127+
128+
### **Go**
129+
130+
```go
131+
/**
132+
* Definition for singly-linked list.
133+
* type ListNode struct {
134+
* Val int
135+
* Next *ListNode
136+
* }
137+
*/
138+
func numComponents(head *ListNode, nums []int) int {
139+
s := map[int]bool{}
140+
for _, v := range nums {
141+
s[v] = true
142+
}
143+
ans := 0
144+
for head != nil {
145+
for head != nil && !s[head.Val] {
146+
head = head.Next
147+
}
148+
if head != nil {
149+
ans++
150+
}
151+
for head != nil && s[head.Val] {
152+
head = head.Next
153+
}
154+
}
155+
return ans
156+
}
157+
```
158+
159+
### **JavaScript**
160+
161+
```js
162+
/**
163+
* Definition for singly-linked list.
164+
* function ListNode(val, next) {
165+
* this.val = (val===undefined ? 0 : val)
166+
* this.next = (next===undefined ? null : next)
167+
* }
168+
*/
169+
/**
170+
* @param {ListNode} head
171+
* @param {number[]} nums
172+
* @return {number}
173+
*/
174+
var numComponents = function (head, nums) {
175+
const s = new Set();
176+
for (const v of nums) {
177+
s.add(v);
178+
}
179+
let ans = 0;
180+
while (head) {
181+
while (head && !s.has(head.val)) {
182+
head = head.next;
183+
}
184+
ans += head != null;
185+
while (head && s.has(head.val)) {
186+
head = head.next;
187+
}
188+
}
189+
return ans;
190+
};
191+
```
192+
105193
### **...**
106194

107195
```
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/**
2+
* Definition for singly-linked list.
3+
* struct ListNode {
4+
* int val;
5+
* ListNode *next;
6+
* ListNode() : val(0), next(nullptr) {}
7+
* ListNode(int x) : val(x), next(nullptr) {}
8+
* ListNode(int x, ListNode *next) : val(x), next(next) {}
9+
* };
10+
*/
11+
class Solution {
12+
public:
13+
int numComponents(ListNode* head, vector<int>& nums) {
14+
unordered_set<int> s(nums.begin(), nums.end());
15+
int ans = 0;
16+
while (head) {
17+
while (head && !s.count(head->val)) head = head->next;
18+
ans += head != nullptr;
19+
while (head && s.count(head->val)) head = head->next;
20+
}
21+
return ans;
22+
}
23+
};
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/**
2+
* Definition for singly-linked list.
3+
* type ListNode struct {
4+
* Val int
5+
* Next *ListNode
6+
* }
7+
*/
8+
func numComponents(head *ListNode, nums []int) int {
9+
s := map[int]bool{}
10+
for _, v := range nums {
11+
s[v] = true
12+
}
13+
ans := 0
14+
for head != nil {
15+
for head != nil && !s[head.Val] {
16+
head = head.Next
17+
}
18+
if head != nil {
19+
ans++
20+
}
21+
for head != nil && s[head.Val] {
22+
head = head.Next
23+
}
24+
}
25+
return ans
26+
}

0 commit comments

Comments
 (0)