Skip to content

Commit aff2030

Browse files
authored
feat: add solutions to lc problem: No.0141 (doocs#1335)
No.0141.Linked List Cycle
1 parent 1cce78a commit aff2030

File tree

5 files changed

+160
-77
lines changed

5 files changed

+160
-77
lines changed

solution/0100-0199/0141.Linked List Cycle/README.md

Lines changed: 67 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -66,11 +66,15 @@
6666

6767
遍历链表,并使用哈希表记录每个节点。当某个节点二次出现时,则表示存在环,直接返回 `true`。否则链表遍历结束,返回 `false`
6868

69+
时间复杂度 $O(n)$,空间复杂度 $O(n)$。其中 $n$ 是链表中的节点数。
70+
6971
**方法二:快慢指针**
7072

71-
定义快慢指针 `slow``fast`,初始指向 `head`
73+
我们定义快慢指针 $fast$ 和 $slow$,初始时均指向 $head$。
74+
75+
快指针每次走两步,慢指针每次走一步,不断循环。当快慢指针相遇时,说明链表存在环。如果循环结束依然没有相遇,说明链表不存在环。
7276

73-
快指针每次走两步,慢指针每次走一步,不断循环。当相遇时,说明链表存在环。如果循环结束依然没有相遇,说明链表不存在环
77+
时间复杂度 $O(n)$,其中 $n$ 是链表中的节点数。空间复杂度 $O(1)$
7478

7579
<!-- tabs:start -->
7680

@@ -156,35 +160,6 @@ public:
156160
};
157161
```
158162
159-
### **JavaScript**
160-
161-
```js
162-
/**
163-
* Definition for singly-linked list.
164-
* function ListNode(val) {
165-
* this.val = val;
166-
* this.next = null;
167-
* }
168-
*/
169-
170-
/**
171-
* @param {ListNode} head
172-
* @return {boolean}
173-
*/
174-
var hasCycle = function (head) {
175-
let slow = head;
176-
let fast = head;
177-
while (fast && fast.next) {
178-
slow = slow.next;
179-
fast = fast.next.next;
180-
if (slow == fast) {
181-
return true;
182-
}
183-
}
184-
return false;
185-
};
186-
```
187-
188163
### **Go**
189164
190165
```go
@@ -250,19 +225,75 @@ function hasCycle(head: ListNode | null): boolean {
250225
*/
251226

252227
function hasCycle(head: ListNode | null): boolean {
253-
if (head == null) {
254-
return false;
255-
}
256228
let slow = head;
257-
let fast = head.next;
229+
let fast = head;
258230
while (fast != null && fast.next != null) {
259-
if (slow == fast) {
231+
slow = slow.next;
232+
fast = fast.next.next;
233+
if (slow === fast) {
260234
return true;
261235
}
236+
}
237+
return false;
238+
}
239+
```
240+
241+
### **JavaScript**
242+
243+
```js
244+
/**
245+
* Definition for singly-linked list.
246+
* function ListNode(val) {
247+
* this.val = val;
248+
* this.next = null;
249+
* }
250+
*/
251+
252+
/**
253+
* @param {ListNode} head
254+
* @return {boolean}
255+
*/
256+
var hasCycle = function (head) {
257+
let slow = head;
258+
let fast = head;
259+
while (fast && fast.next) {
262260
slow = slow.next;
263261
fast = fast.next.next;
262+
if (slow === fast) {
263+
return true;
264+
}
264265
}
265266
return false;
267+
};
268+
```
269+
270+
### **C#**
271+
272+
```cs
273+
/**
274+
* Definition for singly-linked list.
275+
* public class ListNode {
276+
* public int val;
277+
* public ListNode next;
278+
* public ListNode(int x) {
279+
* val = x;
280+
* next = null;
281+
* }
282+
* }
283+
*/
284+
public class Solution {
285+
public bool HasCycle(ListNode head) {
286+
var fast = head;
287+
var slow = head;
288+
while (fast != null && fast.next != null) {
289+
fast = fast.next.next;
290+
slow = slow.next;
291+
if (fast == slow) {
292+
return true;
293+
}
294+
}
295+
return false;
296+
}
266297
}
267298
```
268299

solution/0100-0199/0141.Linked List Cycle/README_EN.md

Lines changed: 61 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -129,35 +129,6 @@ public:
129129
};
130130
```
131131
132-
### **JavaScript**
133-
134-
```js
135-
/**
136-
* Definition for singly-linked list.
137-
* function ListNode(val) {
138-
* this.val = val;
139-
* this.next = null;
140-
* }
141-
*/
142-
143-
/**
144-
* @param {ListNode} head
145-
* @return {boolean}
146-
*/
147-
var hasCycle = function (head) {
148-
let slow = head;
149-
let fast = head;
150-
while (fast && fast.next) {
151-
slow = slow.next;
152-
fast = fast.next.next;
153-
if (slow == fast) {
154-
return true;
155-
}
156-
}
157-
return false;
158-
};
159-
```
160-
161132
### **Go**
162133
163134
```go
@@ -223,19 +194,75 @@ function hasCycle(head: ListNode | null): boolean {
223194
*/
224195

225196
function hasCycle(head: ListNode | null): boolean {
226-
if (head == null) {
227-
return false;
228-
}
229197
let slow = head;
230-
let fast = head.next;
198+
let fast = head;
231199
while (fast != null && fast.next != null) {
232-
if (slow == fast) {
200+
slow = slow.next;
201+
fast = fast.next.next;
202+
if (slow === fast) {
233203
return true;
234204
}
205+
}
206+
return false;
207+
}
208+
```
209+
210+
### **JavaScript**
211+
212+
```js
213+
/**
214+
* Definition for singly-linked list.
215+
* function ListNode(val) {
216+
* this.val = val;
217+
* this.next = null;
218+
* }
219+
*/
220+
221+
/**
222+
* @param {ListNode} head
223+
* @return {boolean}
224+
*/
225+
var hasCycle = function (head) {
226+
let slow = head;
227+
let fast = head;
228+
while (fast && fast.next) {
235229
slow = slow.next;
236230
fast = fast.next.next;
231+
if (slow === fast) {
232+
return true;
233+
}
237234
}
238235
return false;
236+
};
237+
```
238+
239+
### **C#**
240+
241+
```cs
242+
/**
243+
* Definition for singly-linked list.
244+
* public class ListNode {
245+
* public int val;
246+
* public ListNode next;
247+
* public ListNode(int x) {
248+
* val = x;
249+
* next = null;
250+
* }
251+
* }
252+
*/
253+
public class Solution {
254+
public bool HasCycle(ListNode head) {
255+
var fast = head;
256+
var slow = head;
257+
while (fast != null && fast.next != null) {
258+
fast = fast.next.next;
259+
slow = slow.next;
260+
if (fast == slow) {
261+
return true;
262+
}
263+
}
264+
return false;
265+
}
239266
}
240267
```
241268

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/**
2+
* Definition for singly-linked list.
3+
* public class ListNode {
4+
* public int val;
5+
* public ListNode next;
6+
* public ListNode(int x) {
7+
* val = x;
8+
* next = null;
9+
* }
10+
* }
11+
*/
12+
public class Solution {
13+
public bool HasCycle(ListNode head) {
14+
var fast = head;
15+
var slow = head;
16+
while (fast != null && fast.next != null) {
17+
fast = fast.next.next;
18+
slow = slow.next;
19+
if (fast == slow) {
20+
return true;
21+
}
22+
}
23+
return false;
24+
}
25+
}

solution/0100-0199/0141.Linked List Cycle/Solution.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ var hasCycle = function (head) {
1616
while (fast && fast.next) {
1717
slow = slow.next;
1818
fast = fast.next.next;
19-
if (slow == fast) {
19+
if (slow === fast) {
2020
return true;
2121
}
2222
}

solution/0100-0199/0141.Linked List Cycle/Solution.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,14 @@
1111
*/
1212

1313
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)) {
14+
let slow = head;
15+
let fast = head;
16+
while (fast != null && fast.next != null) {
17+
slow = slow.next;
18+
fast = fast.next.next;
19+
if (slow === fast) {
1820
return true;
1921
}
20-
set.add(node);
21-
node = node.next;
2222
}
2323
return false;
2424
}

0 commit comments

Comments
 (0)