File tree Expand file tree Collapse file tree 3 files changed +148
-0
lines changed
solution/0100-0199/0141.Linked List Cycle Expand file tree Collapse file tree 3 files changed +148
-0
lines changed Original file line number Diff line number Diff line change 62
62
63
63
<!-- 这里可写通用的实现逻辑 -->
64
64
65
+ ** 方法一:哈希表**
66
+
67
+ 遍历链表,并使用哈希表记录每个节点。当某个节点二次出现时,则表示存在环,直接返回 ` true ` 。否则链表遍历结束,返回 ` false ` 。
68
+
69
+ ** 方法二:快慢指针**
70
+
65
71
定义快慢指针 ` slow ` 、` fast ` ,初始指向 ` head ` 。
66
72
67
73
快指针每次走两步,慢指针每次走一步,不断循环。当相遇时,说明链表存在环。如果循环结束依然没有相遇,说明链表不存在环。
@@ -200,6 +206,65 @@ func hasCycle(head *ListNode) bool {
200
206
}
201
207
```
202
208
209
+ ### ** TypeScript**
210
+
211
+ ``` ts
212
+ /**
213
+ * Definition for singly-linked list.
214
+ * class ListNode {
215
+ * val: number
216
+ * next: ListNode | null
217
+ * constructor(val?: number, next?: ListNode | null) {
218
+ * this.val = (val===undefined ? 0 : val)
219
+ * this.next = (next===undefined ? null : next)
220
+ * }
221
+ * }
222
+ */
223
+
224
+ function hasCycle(head : ListNode | null ): boolean {
225
+ const set = new Set <ListNode >();
226
+ let node = head ;
227
+ while (node != null ) {
228
+ if (set .has (node )) {
229
+ return true ;
230
+ }
231
+ set .add (node );
232
+ node = node .next ;
233
+ }
234
+ return false ;
235
+ }
236
+ ```
237
+
238
+ ``` ts
239
+ /**
240
+ * Definition for singly-linked list.
241
+ * class ListNode {
242
+ * val: number
243
+ * next: ListNode | null
244
+ * constructor(val?: number, next?: ListNode | null) {
245
+ * this.val = (val===undefined ? 0 : val)
246
+ * this.next = (next===undefined ? null : next)
247
+ * }
248
+ * }
249
+ */
250
+
251
+ function hasCycle(head : ListNode | null ): boolean {
252
+ if (head == null ) {
253
+ return false ;
254
+ }
255
+ let slow = head ;
256
+ let fast = head .next ;
257
+ while (fast != null && fast .next != null ) {
258
+ if (slow == fast ) {
259
+ return true ;
260
+ }
261
+ slow = slow .next ;
262
+ fast = fast .next .next ;
263
+ }
264
+ return false ;
265
+ }
266
+ ```
267
+
203
268
### ** ...**
204
269
205
270
```
Original file line number Diff line number Diff line change @@ -179,6 +179,65 @@ func hasCycle(head *ListNode) bool {
179
179
}
180
180
```
181
181
182
+ ### ** TypeScript**
183
+
184
+ ``` ts
185
+ /**
186
+ * Definition for singly-linked list.
187
+ * class ListNode {
188
+ * val: number
189
+ * next: ListNode | null
190
+ * constructor(val?: number, next?: ListNode | null) {
191
+ * this.val = (val===undefined ? 0 : val)
192
+ * this.next = (next===undefined ? null : next)
193
+ * }
194
+ * }
195
+ */
196
+
197
+ function hasCycle(head : ListNode | null ): boolean {
198
+ const set = new Set <ListNode >();
199
+ let node = head ;
200
+ while (node != null ) {
201
+ if (set .has (node )) {
202
+ return true ;
203
+ }
204
+ set .add (node );
205
+ node = node .next ;
206
+ }
207
+ return false ;
208
+ }
209
+ ```
210
+
211
+ ``` ts
212
+ /**
213
+ * Definition for singly-linked list.
214
+ * class ListNode {
215
+ * val: number
216
+ * next: ListNode | null
217
+ * constructor(val?: number, next?: ListNode | null) {
218
+ * this.val = (val===undefined ? 0 : val)
219
+ * this.next = (next===undefined ? null : next)
220
+ * }
221
+ * }
222
+ */
223
+
224
+ function hasCycle(head : ListNode | null ): boolean {
225
+ if (head == null ) {
226
+ return false ;
227
+ }
228
+ let slow = head ;
229
+ let fast = head .next ;
230
+ while (fast != null && fast .next != null ) {
231
+ if (slow == fast ) {
232
+ return true ;
233
+ }
234
+ slow = slow .next ;
235
+ fast = fast .next .next ;
236
+ }
237
+ return false ;
238
+ }
239
+ ```
240
+
182
241
### ** ...**
183
242
184
243
```
Original file line number Diff line number Diff line change
1
+ /**
2
+ * Definition for singly-linked list.
3
+ * class ListNode {
4
+ * val: number
5
+ * next: ListNode | null
6
+ * constructor(val?: number, next?: ListNode | null) {
7
+ * this.val = (val===undefined ? 0 : val)
8
+ * this.next = (next===undefined ? null : next)
9
+ * }
10
+ * }
11
+ */
12
+
13
+ function hasCycle ( head : ListNode | null ) : boolean {
14
+ const set = new Set < ListNode > ( ) ;
15
+ let node = head ;
16
+ while ( node != null ) {
17
+ if ( set . has ( node ) ) {
18
+ return true ;
19
+ }
20
+ set . add ( node ) ;
21
+ node = node . next ;
22
+ }
23
+ return false ;
24
+ }
You can’t perform that action at this time.
0 commit comments