Files
LeetCode-Go/leetcode/0142.Linked-List-Cycle-II/142. Linked List Cycle II.go
2020-08-07 17:06:53 +08:00

45 lines
750 B
Go

package leetcode
import (
"github.com/halfrost/LeetCode-Go/structures"
)
// ListNode define
type ListNode = structures.ListNode
/**
* Definition for singly-linked list.
* type ListNode struct {
* Val int
* Next *ListNode
* }
*/
func detectCycle(head *ListNode) *ListNode {
if head == nil || head.Next == nil {
return nil
}
isCycle, slow := hasCycle142(head)
if !isCycle {
return nil
}
fast := head
for fast != slow {
fast = fast.Next
slow = slow.Next
}
return fast
}
func hasCycle142(head *ListNode) (bool, *ListNode) {
fast := head
slow := head
for slow != nil && fast != nil && fast.Next != nil {
fast = fast.Next.Next
slow = slow.Next
if fast == slow {
return true, slow
}
}
return false, nil
}