Linked List Cycle II
Example 1:

Example 2:

Example 3:

Constraints:
My Solution
Last updated



Last updated
func detectCycle(head *ListNode) *ListNode {
slow, fast := head, head
hasCycle := false
for fast != nil && fast.Next != nil {
fast = fast.Next.Next
slow = slow.Next
if slow == fast {
hasCycle = true
break
}
}
if !hasCycle {
return nil
}
for slow != head {
slow = slow.Next
head = head.Next
}
return head
}