Last active
September 15, 2020 02:33
-
-
Save GAierken/5e66d1fbf3e4c5b7adf367520e46004f to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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