Skip to content

Commit 4806eac

Browse files
committed
feat: add ts solution to lc problem: No.0141
No.0141.Linked List Cycle
1 parent c60d7e1 commit 4806eac

File tree

3 files changed

+148
-0
lines changed

3 files changed

+148
-0
lines changed

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

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,12 @@
6262

6363
<!-- 这里可写通用的实现逻辑 -->
6464

65+
**方法一:哈希表**
66+
67+
遍历链表,并使用哈希表记录每个节点。当某个节点二次出现时,则表示存在环,直接返回 `true`。否则链表遍历结束,返回 `false`
68+
69+
**方法二:快慢指针**
70+
6571
定义快慢指针 `slow``fast`,初始指向 `head`
6672

6773
快指针每次走两步,慢指针每次走一步,不断循环。当相遇时,说明链表存在环。如果循环结束依然没有相遇,说明链表不存在环。
@@ -200,6 +206,65 @@ func hasCycle(head *ListNode) bool {
200206
}
201207
```
202208

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+
203268
### **...**
204269

205270
```

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

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,65 @@ func hasCycle(head *ListNode) bool {
179179
}
180180
```
181181

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+
182241
### **...**
183242

184243
```
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
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+
}

0 commit comments

Comments
 (0)