pull/33/head
jaredliw 2021-07-13 21:06:03 +08:00
parent 4a4d5271e5
commit 2e9819611c
1 changed files with 20 additions and 6 deletions

View File

@ -10,7 +10,7 @@
####
> pos-1
> pos-1
>
> true false
@ -28,7 +28,7 @@
![](https://img-blog.csdnimg.cn/20210321132015849.png)
\
****
@ -42,7 +42,6 @@ Java Code:
```java
public class Solution {
public boolean hasCycle(ListNode head) {
ListNode fast = head;
ListNode low = head;
while (fast != null && fast.next != null) {
@ -80,11 +79,11 @@ class Solution {
public:
bool hasCycle(ListNode *head) {
ListNode * fast = head;
ListNode * low = head;
ListNode * slow = head;
while (fast != nullptr && fast->next != nullptr) {
fast = fast->next->next;
low = low->next;
if (fast == low) {
slow = slow->next;
if (fast == slow) {
return true;
}
}
@ -93,3 +92,18 @@ public:
};
```
Python Code:
```py
class Solution:
def hasCycle(self, head: ListNode) -> bool:
fast = head
slow = head
while fast and fast.next:
fast = fast.next.next
low = low.next
if fast == low:
return True
return False
```