From aaad0cb27b75a625f98184f987ffd5eacd628682 Mon Sep 17 00:00:00 2001 From: mrwolferinc <66963621+mrwolferinc@users.noreply.github.com> Date: Mon, 21 Dec 2020 15:51:03 -0500 Subject: [PATCH 1/3] Initial commit --- .../linked-list/tortoise-and-hare/README.md | 13 +++++++++++++ 1 file changed, 13 insertions(+) create mode 100644 src/algorithms/linked-list/tortoise-and-hare/README.md diff --git a/src/algorithms/linked-list/tortoise-and-hare/README.md b/src/algorithms/linked-list/tortoise-and-hare/README.md new file mode 100644 index 00000000..ad72e5c6 --- /dev/null +++ b/src/algorithms/linked-list/tortoise-and-hare/README.md @@ -0,0 +1,13 @@ +# Floyd's Tortoise and Hare +**Floyd's cycle-finding algorithm** is a pointer algorithm that is used for finding a cycle in a linked list. It uses only two pointers, each moving at different speeds. It is also called the "tortoise and the hare algorithm", alluding to Aseop's fable of [The Tortoise and the Hare](https://en.wikipedia.org/wiki/The_Tortoise_and_the_Hare). + +## Main algorithm +1. Create two pointers, which are the tortoise and the hare. Tortoise begins at the head, while hare begins at `head.next`. +2. Since hare and `hare.next` are not null, enter the while loop. Tortoise and hare are not equal to each other, so we'll move both of them over. Tortoise gets moved one spot, while hare gets moved two spots. +3. Repeat step two until the while loop is still true and tortoise and hare are equal to each other. + * If tortoise and hare are equal to eachother, then return true because a cycle was found. + * Otherwise, if the while loop cannot continue because hare and/or `hare.next` is null, then return false because no cycle was found. + +## References +* [Wikipedia](https://en.wikipedia.org/wiki/Cycle_detection#Floyd's_Tortoise_and_Hare) +* [Floyd's Tortoise and Hare Algorithm: Finding a Cycle in a Linked List](https://dev.to/alisabaj/floyd-s-tortoise-and-hare-algorithm-finding-a-cycle-in-a-linked-list-39af) From 29bd6c8c1cec283535746513edaa5dd512891b9a Mon Sep 17 00:00:00 2001 From: mrwolferinc <66963621+mrwolferinc@users.noreply.github.com> Date: Mon, 21 Dec 2020 15:54:36 -0500 Subject: [PATCH 2/3] Created hasCycle.js --- .../linked-list/tortoise-and-hare/hasCycle.js | 27 +++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 src/algorithms/linked-list/tortoise-and-hare/hasCycle.js diff --git a/src/algorithms/linked-list/tortoise-and-hare/hasCycle.js b/src/algorithms/linked-list/tortoise-and-hare/hasCycle.js new file mode 100644 index 00000000..12a0aa04 --- /dev/null +++ b/src/algorithms/linked-list/tortoise-and-hare/hasCycle.js @@ -0,0 +1,27 @@ +/** + * @param {object} linkedList - The linked list to check + * @returns {boolean} + */ +export default function hasCycle(linkedList) { + const head = linkedList.head; + + // Check if head and head.next exist + if (!head || !head.next) { + return false; // No cycle was found + } + + let tortoise = head; + let hare = head.next; + + while (hare && hare.next) { + // Check if tortoise and hare are equal to each other + if (tortoise === hare) { + return true; // A cycle was found + } + + tortoise = tortoise.next; + hare = hare.next.next; + } + + return false; // No cycle was found +} From 95b98d08a70315f4cd3b3351e2e803730203c189 Mon Sep 17 00:00:00 2001 From: mrwolferinc <66963621+mrwolferinc@users.noreply.github.com> Date: Mon, 21 Dec 2020 15:57:12 -0500 Subject: [PATCH 3/3] Updated README.md --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index feb9832d..55ef4e02 100644 --- a/README.md +++ b/README.md @@ -120,6 +120,7 @@ a set of rules that precisely define a sequence of operations. * `B` [Counting Sort](src/algorithms/sorting/counting-sort) * `B` [Radix Sort](src/algorithms/sorting/radix-sort) * **Linked Lists** + * `B` [Floyd's Tortoise And Hare](src/algorithms/linked-list/tortoise-and-hare) - find a cycle in a linked list * `B` [Straight Traversal](src/algorithms/linked-list/traversal) * `B` [Reverse Traversal](src/algorithms/linked-list/reverse-traversal) * **Trees**