Skip to content

Instantly share code, notes, and snippets.

@GAierken
Last active September 15, 2020 02:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save GAierken/5e66d1fbf3e4c5b7adf367520e46004f to your computer and use it in GitHub Desktop.
Save GAierken/5e66d1fbf3e4c5b7adf367520e46004f to your computer and use it in GitHub Desktop.
const hasCycle = (head) => {
// initial with fast and slow pointers with head
let slow = head
let fast = head
//traverse linked list
while(fast && fast.next){
// fast moves by two
fast = fast.next.next
// slow moves by one
slow = slow.next
// two pointers meet, cycle
if(fast === slow){
return true
}
}
return false
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment