更新 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 Go
```func detectCycle(head *ListNode) *ListNode { ```go
if head ==nil{ func detectCycle(head *ListNode) *ListNode {
return head slow, fast := head, head
} for fast != nil && fast.Next != nil {
slow:=head slow = slow.Next
fast:=head.Next fast = fast.Next.Next
if slow == fast {
for fast!=nil&&fast.Next!=nil{ for slow != head {
if fast==slow{ slow = slow.Next
slow=head head = head.Next
fast=fast.Next
for fast!=slow {
fast=fast.Next
slow=slow.Next
} }
return slow return head
} }
fast=fast.Next.Next
slow=slow.Next
} }
return nil return nil
} }