mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-09 19:44:45 +08:00
@ -234,6 +234,29 @@ class Solution:
|
|||||||
```
|
```
|
||||||
|
|
||||||
Go:
|
Go:
|
||||||
|
```func detectCycle(head *ListNode) *ListNode {
|
||||||
|
if head ==nil{
|
||||||
|
return head
|
||||||
|
}
|
||||||
|
slow:=head
|
||||||
|
fast:=head.Next
|
||||||
|
|
||||||
|
for fast!=nil&&fast.Next!=nil{
|
||||||
|
if fast==slow{
|
||||||
|
slow=head
|
||||||
|
fast=fast.Next
|
||||||
|
for fast!=slow {
|
||||||
|
fast=fast.Next
|
||||||
|
slow=slow.Next
|
||||||
|
}
|
||||||
|
return slow
|
||||||
|
}
|
||||||
|
fast=fast.Next.Next
|
||||||
|
slow=slow.Next
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
-----------------------
|
-----------------------
|
||||||
|
Reference in New Issue
Block a user