添加(0141.环形链表.md):增加go版本
This commit is contained in:
parent
79b2453233
commit
9dbc51455d
|
|
@ -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
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue