Skip to content

Instantly share code, notes, and snippets.

@jordanrios94
Created February 3, 2023 10:14
Show Gist options
  • Save jordanrios94/05766f14bd838ca5796b0e06a717ab4f to your computer and use it in GitHub Desktop.
Save jordanrios94/05766f14bd838ca5796b0e06a717ab4f to your computer and use it in GitHub Desktop.
LinkedList Circular
/*
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