mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-09 19:44:45 +08:00
更新 0142.环形链表II go版本
1、原先的Go版本代码显示错乱,一部分代码未显示 2、去掉多余判断,让代码更简洁
This commit is contained in:
@ -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
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user