Skip to content

Instantly share code, notes, and snippets.

@kevinahn7
Last active July 21, 2021 01:46
Show Gist options
  • Select an option

  • Save kevinahn7/67ecb803bc7ff5a119a65c17df088ac4 to your computer and use it in GitHub Desktop.

Select an option

Save kevinahn7/67ecb803bc7ff5a119a65c17df088ac4 to your computer and use it in GitHub Desktop.
function checkForLoopage(node) {
if (node === null) return false;
let fast = node.next;
let slow = node;
while (fast !== null && fast.next !== null) {
if (fast === slow) return true;
else {
fast = fast.next.next;
slow = slow.next;
}
}
return false;
}
OR
var hasCycle = function(head) {
let fastPointer = head;
let slowPointer = head;
while (fastPointer && fastPointer.next) {
fastPointer = fastPointer.next.next;
slowPointer = slowPointer.next;;
if (fastPointer === slowPointer) {
return true
}
}
return false;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment