mirror of
https://github.com/chefyuan/algorithm-base.git
synced 2025-08-07 15:52:12 +00:00
添加Go语言题解
This commit is contained in:
@@ -297,3 +297,29 @@ class Solution {
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
|
||||
Go Code:
|
||||
|
||||
```go
|
||||
func detectCycle(head *ListNode) *ListNode {
|
||||
if head == nil { return nil }
|
||||
s, f := head, head
|
||||
for f != nil && f.Next != nil {
|
||||
s = s.Next
|
||||
f = f.Next.Next
|
||||
// 快慢指针相遇
|
||||
if f == s {
|
||||
// 快指针从头开始一步一步走,也可以用一个新的指针
|
||||
f = head
|
||||
for f != s {
|
||||
f = f.Next
|
||||
s = s.Next
|
||||
}
|
||||
return f
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
```
|
||||
|
||||
|
Reference in New Issue
Block a user