Skip to content

Instantly share code, notes, and snippets.

@alldroll
Created March 28, 2020 22:05
Show Gist options
  • Select an option

  • Save alldroll/c290ba49cfc9df38dcd260a50c3d6ac0 to your computer and use it in GitHub Desktop.

Select an option

Save alldroll/c290ba49cfc9df38dcd260a50c3d6ac0 to your computer and use it in GitHub Desktop.
// https://leetcode.com/problems/linked-list-cycle/
/**
* Definition for singly-linked list.
* type ListNode struct {
* Val int
* Next *ListNode
* }
*/
func hasCycle(head *ListNode) bool {
hare := head
tortoise := head
for hare != nil && hare.Next != nil {
hare = hare.Next.Next
tortoise = tortoise.Next
if hare != nil && hare.Val == tortoise.Val {
return true
}
}
return false
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment