Skip to content

Commit 2e98196

Browse files
committed
添加py
1 parent 4a4d527 commit 2e98196

File tree

1 file changed

+20
-6
lines changed

1 file changed

+20
-6
lines changed

animation-simulation/链表篇/leetcode141环形链表.md

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
#### 题目描述
1212

13-
> 给定一个链表,判断链表中是否有环。pos代表环的入口,若为-1,则代表无环
13+
> 给定一个链表,判断链表中是否有环。pos代表环的入口,若为-1,则代表无环
1414
>
1515
> 如果链表中存在环,则返回 true 。 否则,返回 false 。
1616
@@ -28,7 +28,7 @@
2828

2929
![在这里插入图片描述](https://img-blog.csdnimg.cn/20210321132015849.png)
3030

31-
好啦,做题思路已经有了,让我们一起看一下代码的执行过程吧。\
31+
好啦,做题思路已经有了,让我们一起看一下代码的执行过程吧。
3232

3333
**动画模拟**
3434

@@ -42,7 +42,6 @@ Java Code:
4242
```java
4343
public class Solution {
4444
public boolean hasCycle(ListNode head) {
45-
4645
ListNode fast = head;
4746
ListNode low = head;
4847
while (fast != null && fast.next != null) {
@@ -80,11 +79,11 @@ class Solution {
8079
public:
8180
bool hasCycle(ListNode *head) {
8281
ListNode * fast = head;
83-
ListNode * low = head;
82+
ListNode * slow = head;
8483
while (fast != nullptr && fast->next != nullptr) {
8584
fast = fast->next->next;
86-
low = low->next;
87-
if (fast == low) {
85+
slow = slow->next;
86+
if (fast == slow) {
8887
return true;
8988
}
9089
}
@@ -93,3 +92,18 @@ public:
9392
};
9493
```
9594
95+
Python Code:
96+
97+
```py
98+
class Solution:
99+
def hasCycle(self, head: ListNode) -> bool:
100+
fast = head
101+
slow = head
102+
while fast and fast.next:
103+
fast = fast.next.next
104+
low = low.next
105+
if fast == low:
106+
return True
107+
return False
108+
```
109+

0 commit comments

Comments
 (0)