Last active
July 21, 2021 01:46
-
-
Save kevinahn7/67ecb803bc7ff5a119a65c17df088ac4 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| 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