Created
January 20, 2020 12:43
-
-
Save dashsaurabh/292d52495e0d3017da1d3f7eaea717c4 to your computer and use it in GitHub Desktop.
Detect Loop in Linked List
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
var hasCycle = function(head) { | |
if (head === null) return false; | |
if (head.next === null) return false; | |
let slow = head; | |
let fast = head.next; | |
while(fast !== null && fast.next !== null){ | |
slow = slow.next; | |
fast = fast.next.next; | |
if(slow === fast) { | |
return true; | |
} | |
} | |
return false; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment