Created
February 3, 2023 10:14
-
-
Save jordanrios94/05766f14bd838ca5796b0e06a717ab4f to your computer and use it in GitHub Desktop.
LinkedList Circular
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
/* | |
Given a linked list, return true if the list is circular, false if it is not. | |
*/ | |
function circular(list) { | |
let slow = list.getFirst(); | |
let fast = list.getFirst(); | |
for (let item of list) { | |
if (slow.next && fast.next) { | |
slow = slow.next; | |
fast = fast.next.next; | |
if (slow === fast) { | |
return true; | |
} | |
} | |
} | |
return false; | |
} | |
function circular(list) { | |
let slow = list.getFirst(); | |
let fast = list.getFirst(); | |
while (fast.next && fast.next.next) { | |
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