Skip to content

Instantly share code, notes, and snippets.

@dashsaurabh
Created January 20, 2020 12:43
Show Gist options
  • Save dashsaurabh/292d52495e0d3017da1d3f7eaea717c4 to your computer and use it in GitHub Desktop.
Save dashsaurabh/292d52495e0d3017da1d3f7eaea717c4 to your computer and use it in GitHub Desktop.
Detect Loop in Linked List
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