29 lines
412 B
Go
29 lines
412 B
Go
// Package q141 implements a solution for https://leetcode.com/problems/linked-list-cycle/
|
|
package q141
|
|
|
|
type ListNode struct {
|
|
Val int
|
|
Next *ListNode
|
|
}
|
|
|
|
func hasCycle(head *ListNode) bool {
|
|
if head == nil {
|
|
return false
|
|
}
|
|
|
|
p1, p2 := head, head
|
|
for {
|
|
for range 2 {
|
|
p2 = p2.Next
|
|
switch p2 {
|
|
case nil:
|
|
return false
|
|
case p1:
|
|
return true
|
|
}
|
|
}
|
|
p1 = p1.Next
|
|
}
|
|
}
|
|
|
|
var _ = hasCycle
|