更新 0142.环形链表II go版本

1、原先的Go版本代码显示错乱,一部分代码未显示
2、去掉多余判断,让代码更简洁
This commit is contained in:
NevS
2021-05-28 00:33:17 +08:00
committed by GitHub
parent d7235d9f51
commit c722642392

View File

@ -234,25 +234,19 @@ class Solution:
```
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
```go
func detectCycle(head *ListNode) *ListNode {
slow, fast := head, head
for fast != nil && fast.Next != nil {
slow = slow.Next
fast = fast.Next.Next
if slow == fast {
for slow != head {
slow = slow.Next
head = head.Next
}
return slow
return head
}
fast=fast.Next.Next
slow=slow.Next
}
return nil
}