From 84d87ec0f2d25551dfdf2b99c017fe4ec86c660c Mon Sep 17 00:00:00 2001 From: TejasSingh022 <157150137+TejasSingh022@users.noreply.github.com> Date: Fri, 10 Oct 2025 14:59:12 +0530 Subject: [PATCH 01/10] Create TortoiseHareAlgorithm.java Implement TortoiseHareAlgo with append, getMiddle, and toString methods - Added generic singly linked list with inner Node class - Implemented append() to add elements - Implemented getMiddle() using Tortoise-Hare approach - Added toString() for readable list representation --- .../lists/TortoiseHareAlgorithm.java | 59 +++++++++++++++++++ 1 file changed, 59 insertions(+) create mode 100644 src/main/java/com/thealgorithms/datastructures/lists/TortoiseHareAlgorithm.java diff --git a/src/main/java/com/thealgorithms/datastructures/lists/TortoiseHareAlgorithm.java b/src/main/java/com/thealgorithms/datastructures/lists/TortoiseHareAlgorithm.java new file mode 100644 index 000000000000..39694901f6fe --- /dev/null +++ b/src/main/java/com/thealgorithms/datastructures/lists/TortoiseHareAlgorithm.java @@ -0,0 +1,59 @@ +package com.thealgorithms.datastructures.lists; + +public class TortoiseHareAlgo { + static final class Node { + Node next; + E value; + + private Node(E value, Node next) { + this.value = value; + this.next = next; + } + } + + private Node head = null; + + public TortoiseHareAlgo() { + head = null; + } + + public void append(E value) { + Node newNode = new Node<>(value, null); + if (head == null) { + head = newNode; + return; + } + Node current = head; + while (current.next != null) { + current = current.next; + } + current.next = newNode; + } + + public E getMiddle() { + if (head == null) return null; + + Node slow = head; + Node fast = head; + + while (fast != null && fast.next != null) { + slow = slow.next; + fast = fast.next.next; + } + + return slow.value; + } + + @Override + public String toString() { + StringBuilder sb = new StringBuilder("["); + Node current = head; + while (current != null) { + sb.append(current.value); + if (current.next != null) sb.append(", "); + current = current.next; + } + sb.append("]"); + return sb.toString(); + } +} From da0b0f16a7af3473dea2ee8837579cb780aa625f Mon Sep 17 00:00:00 2001 From: TejasSingh022 <157150137+TejasSingh022@users.noreply.github.com> Date: Fri, 10 Oct 2025 15:00:30 +0530 Subject: [PATCH 02/10] Create TortoiseHareAlgoTest.java Add JUnit tests for TortoiseHareAlgo - Verified append() and toString() output - Tested getMiddle() for odd, even, and empty lists - Ensured correct behavior and null handling --- .../lists/TortoiseHareAlgoTest.java | 44 +++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100644 src/test/java/com/thealgorithms/datastructures/lists/TortoiseHareAlgoTest.java diff --git a/src/test/java/com/thealgorithms/datastructures/lists/TortoiseHareAlgoTest.java b/src/test/java/com/thealgorithms/datastructures/lists/TortoiseHareAlgoTest.java new file mode 100644 index 000000000000..f0cc99c0e8ba --- /dev/null +++ b/src/test/java/com/thealgorithms/datastructures/lists/TortoiseHareAlgoTest.java @@ -0,0 +1,44 @@ +package com.thealgorithms.datastructures.lists; + +import org.junit.jupiter.api.Test; +import static org.junit.jupiter.api.Assertions.*; + +class TortoiseHareAlgoTest { + + @Test + void testAppendAndToString() { + TortoiseHareAlgo list = new TortoiseHareAlgo<>(); + list.append(10); + list.append(20); + list.append(30); + assertEquals("[10, 20, 30]", list.toString()); + } + + @Test + void testGetMiddleOdd() { + TortoiseHareAlgo list = new TortoiseHareAlgo<>(); + list.append(1); + list.append(2); + list.append(3); + list.append(4); + list.append(5); + assertEquals(3, list.getMiddle()); + } + + @Test + void testGetMiddleEven() { + TortoiseHareAlgo list = new TortoiseHareAlgo<>(); + list.append(1); + list.append(2); + list.append(3); + list.append(4); + assertEquals(3, list.getMiddle()); // returns second middle + } + + @Test + void testEmptyList() { + TortoiseHareAlgo list = new TortoiseHareAlgo<>(); + assertNull(list.getMiddle()); + assertEquals("[]", list.toString()); + } +} From 15f2e559387cf9a3a0939e5243940c58c66037c3 Mon Sep 17 00:00:00 2001 From: TejasSingh022 <157150137+TejasSingh022@users.noreply.github.com> Date: Fri, 10 Oct 2025 15:01:43 +0530 Subject: [PATCH 03/10] Update README.md Add TortoiseHareAlgo to linked list documentation - Added TortoiseHareAlgo.java to file descriptions - Described its purpose: finding middle element using Tortoise-Hare algorithm --- src/main/java/com/thealgorithms/datastructures/lists/README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/src/main/java/com/thealgorithms/datastructures/lists/README.md b/src/main/java/com/thealgorithms/datastructures/lists/README.md index 6aefa4c98e6d..5a19c3bfa990 100644 --- a/src/main/java/com/thealgorithms/datastructures/lists/README.md +++ b/src/main/java/com/thealgorithms/datastructures/lists/README.md @@ -30,3 +30,4 @@ The `next` variable points to the next node in the data structure and value stor 6. `MergeKSortedLinkedlist.java` : Merges K sorted linked list with mergesort (mergesort is also the most efficient sorting algorithm for linked list). 7. `RandomNode.java` : Selects a random node from given linked list and diplays it. 8. `SkipList.java` : Data Structure used for storing a sorted list of elements with help of a Linked list hierarchy that connects to subsequences of elements. +9. `TortoiseHareAlgo.java` : Finds the middle element of a linked list using the fast and slow pointer (Tortoise-Hare) algorithm. From 15a2d1b33d38c58777f062826013a91307a7675a Mon Sep 17 00:00:00 2001 From: TejasSingh022 <157150137+TejasSingh022@users.noreply.github.com> Date: Fri, 10 Oct 2025 15:13:39 +0530 Subject: [PATCH 04/10] Rename TortoiseHareAlgorithm.java to TortoiseHareAlgo.java Fixed build error --- .../lists/{TortoiseHareAlgorithm.java => TortoiseHareAlgo.java} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename src/main/java/com/thealgorithms/datastructures/lists/{TortoiseHareAlgorithm.java => TortoiseHareAlgo.java} (100%) diff --git a/src/main/java/com/thealgorithms/datastructures/lists/TortoiseHareAlgorithm.java b/src/main/java/com/thealgorithms/datastructures/lists/TortoiseHareAlgo.java similarity index 100% rename from src/main/java/com/thealgorithms/datastructures/lists/TortoiseHareAlgorithm.java rename to src/main/java/com/thealgorithms/datastructures/lists/TortoiseHareAlgo.java From fc1eaff197e4ee420ffa9e033525b656a1d6f28c Mon Sep 17 00:00:00 2001 From: TejasSingh022 <157150137+TejasSingh022@users.noreply.github.com> Date: Fri, 10 Oct 2025 15:14:31 +0530 Subject: [PATCH 05/10] Update TortoiseHareAlgoTest.java Fixed line formatting build error --- .../datastructures/lists/TortoiseHareAlgoTest.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/test/java/com/thealgorithms/datastructures/lists/TortoiseHareAlgoTest.java b/src/test/java/com/thealgorithms/datastructures/lists/TortoiseHareAlgoTest.java index f0cc99c0e8ba..528f61dd8d68 100644 --- a/src/test/java/com/thealgorithms/datastructures/lists/TortoiseHareAlgoTest.java +++ b/src/test/java/com/thealgorithms/datastructures/lists/TortoiseHareAlgoTest.java @@ -1,7 +1,7 @@ package com.thealgorithms.datastructures.lists; -import org.junit.jupiter.api.Test; import static org.junit.jupiter.api.Assertions.*; +import org.junit.jupiter.api.Test; class TortoiseHareAlgoTest { From 9df52d580e85d90eb3ce4b6b2086b02f0f28ed4c Mon Sep 17 00:00:00 2001 From: TejasSingh022 <157150137+TejasSingh022@users.noreply.github.com> Date: Fri, 10 Oct 2025 15:18:09 +0530 Subject: [PATCH 06/10] Update TortoiseHareAlgoTest.java Fixed line formatting build error --- .../thealgorithms/datastructures/lists/TortoiseHareAlgoTest.java | 1 + 1 file changed, 1 insertion(+) diff --git a/src/test/java/com/thealgorithms/datastructures/lists/TortoiseHareAlgoTest.java b/src/test/java/com/thealgorithms/datastructures/lists/TortoiseHareAlgoTest.java index 528f61dd8d68..c2e7b5f8cd88 100644 --- a/src/test/java/com/thealgorithms/datastructures/lists/TortoiseHareAlgoTest.java +++ b/src/test/java/com/thealgorithms/datastructures/lists/TortoiseHareAlgoTest.java @@ -1,6 +1,7 @@ package com.thealgorithms.datastructures.lists; import static org.junit.jupiter.api.Assertions.*; + import org.junit.jupiter.api.Test; class TortoiseHareAlgoTest { From a90f5341c7f9bb41b093e8a9e61d4d8ae26ad42d Mon Sep 17 00:00:00 2001 From: TejasSingh022 <157150137+TejasSingh022@users.noreply.github.com> Date: Fri, 10 Oct 2025 15:22:08 +0530 Subject: [PATCH 07/10] Update TortoiseHareAlgo.java Added {} after if statement instead of directly writing statement --- .../thealgorithms/datastructures/lists/TortoiseHareAlgo.java | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/main/java/com/thealgorithms/datastructures/lists/TortoiseHareAlgo.java b/src/main/java/com/thealgorithms/datastructures/lists/TortoiseHareAlgo.java index 39694901f6fe..6ea3eb6bd292 100644 --- a/src/main/java/com/thealgorithms/datastructures/lists/TortoiseHareAlgo.java +++ b/src/main/java/com/thealgorithms/datastructures/lists/TortoiseHareAlgo.java @@ -31,7 +31,9 @@ public void append(E value) { } public E getMiddle() { - if (head == null) return null; + if (head == null){ + return null; + } Node slow = head; Node fast = head; From 5d3ba751687db5a0610c07187db895aceb179704 Mon Sep 17 00:00:00 2001 From: TejasSingh022 <157150137+TejasSingh022@users.noreply.github.com> Date: Fri, 10 Oct 2025 15:23:56 +0530 Subject: [PATCH 08/10] Update TortoiseHareAlgo.java Fixed line formatting build error --- .../thealgorithms/datastructures/lists/TortoiseHareAlgo.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/com/thealgorithms/datastructures/lists/TortoiseHareAlgo.java b/src/main/java/com/thealgorithms/datastructures/lists/TortoiseHareAlgo.java index 6ea3eb6bd292..63018c4366bd 100644 --- a/src/main/java/com/thealgorithms/datastructures/lists/TortoiseHareAlgo.java +++ b/src/main/java/com/thealgorithms/datastructures/lists/TortoiseHareAlgo.java @@ -31,7 +31,7 @@ public void append(E value) { } public E getMiddle() { - if (head == null){ + if (head == null) { return null; } From e82762ac30104744d35bf1b6a36d7854035dea3f Mon Sep 17 00:00:00 2001 From: TejasSingh022 <157150137+TejasSingh022@users.noreply.github.com> Date: Fri, 10 Oct 2025 15:29:12 +0530 Subject: [PATCH 09/10] Update TortoiseHareAlgo.java Added {} after if statement instead of directly writing statement --- .../thealgorithms/datastructures/lists/TortoiseHareAlgo.java | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/main/java/com/thealgorithms/datastructures/lists/TortoiseHareAlgo.java b/src/main/java/com/thealgorithms/datastructures/lists/TortoiseHareAlgo.java index 63018c4366bd..9d803003c658 100644 --- a/src/main/java/com/thealgorithms/datastructures/lists/TortoiseHareAlgo.java +++ b/src/main/java/com/thealgorithms/datastructures/lists/TortoiseHareAlgo.java @@ -52,7 +52,9 @@ public String toString() { Node current = head; while (current != null) { sb.append(current.value); - if (current.next != null) sb.append(", "); + if (current.next != null) { + sb.append(", "); + } current = current.next; } sb.append("]"); From 41214b4be27943a463a70b65af59f0140c39c2cd Mon Sep 17 00:00:00 2001 From: TejasSingh022 <157150137+TejasSingh022@users.noreply.github.com> Date: Fri, 10 Oct 2025 15:34:43 +0530 Subject: [PATCH 10/10] Update TortoiseHareAlgoTest.java Replace .* import with specific imports --- .../datastructures/lists/TortoiseHareAlgoTest.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/test/java/com/thealgorithms/datastructures/lists/TortoiseHareAlgoTest.java b/src/test/java/com/thealgorithms/datastructures/lists/TortoiseHareAlgoTest.java index c2e7b5f8cd88..2804534c988c 100644 --- a/src/test/java/com/thealgorithms/datastructures/lists/TortoiseHareAlgoTest.java +++ b/src/test/java/com/thealgorithms/datastructures/lists/TortoiseHareAlgoTest.java @@ -1,6 +1,7 @@ package com.thealgorithms.datastructures.lists; -import static org.junit.jupiter.api.Assertions.*; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNull; import org.junit.jupiter.api.Test;