mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-11 04:54:51 +08:00
添加(0141.环形链表.md):增加go版本
This commit is contained in:
@ -106,6 +106,21 @@ class Solution:
|
|||||||
## Go
|
## Go
|
||||||
|
|
||||||
```go
|
```go
|
||||||
|
func hasCycle(head *ListNode) bool {
|
||||||
|
if head==nil{
|
||||||
|
return false
|
||||||
|
} //空链表一定不会有环
|
||||||
|
fast:=head
|
||||||
|
slow:=head //快慢指针
|
||||||
|
for fast.Next!=nil&&fast.Next.Next!=nil{
|
||||||
|
fast=fast.Next.Next
|
||||||
|
slow=slow.Next
|
||||||
|
if fast==slow{
|
||||||
|
return true //快慢指针相遇则有环
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
### JavaScript
|
### JavaScript
|
||||||
|
Reference in New Issue
Block a user