Skip to content

Commit a935710

Browse files
Add TortoiseHareAlgo implementation with append, toString, and getMiddle methods (TheAlgorithms#6722)
* 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 * 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 * 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 * Rename TortoiseHareAlgorithm.java to TortoiseHareAlgo.java Fixed build error * Update TortoiseHareAlgoTest.java Fixed line formatting build error * Update TortoiseHareAlgoTest.java Fixed line formatting build error * Update TortoiseHareAlgo.java Added {} after if statement instead of directly writing statement * Update TortoiseHareAlgo.java Fixed line formatting build error * Update TortoiseHareAlgo.java Added {} after if statement instead of directly writing statement * Update TortoiseHareAlgoTest.java Replace .* import with specific imports --------- Co-authored-by: Deniz Altunkapan <deniz.altunkapan@outlook.com>
1 parent 87ad52c commit a935710

File tree

3 files changed

+110
-0
lines changed

3 files changed

+110
-0
lines changed

src/main/java/com/thealgorithms/datastructures/lists/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,3 +30,4 @@ The `next` variable points to the next node in the data structure and value stor
3030
6. `MergeKSortedLinkedlist.java` : Merges K sorted linked list with mergesort (mergesort is also the most efficient sorting algorithm for linked list).
3131
7. `RandomNode.java` : Selects a random node from given linked list and diplays it.
3232
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.
33+
9. `TortoiseHareAlgo.java` : Finds the middle element of a linked list using the fast and slow pointer (Tortoise-Hare) algorithm.
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
package com.thealgorithms.datastructures.lists;
2+
3+
public class TortoiseHareAlgo<E> {
4+
static final class Node<E> {
5+
Node<E> next;
6+
E value;
7+
8+
private Node(E value, Node<E> next) {
9+
this.value = value;
10+
this.next = next;
11+
}
12+
}
13+
14+
private Node<E> head = null;
15+
16+
public TortoiseHareAlgo() {
17+
head = null;
18+
}
19+
20+
public void append(E value) {
21+
Node<E> newNode = new Node<>(value, null);
22+
if (head == null) {
23+
head = newNode;
24+
return;
25+
}
26+
Node<E> current = head;
27+
while (current.next != null) {
28+
current = current.next;
29+
}
30+
current.next = newNode;
31+
}
32+
33+
public E getMiddle() {
34+
if (head == null) {
35+
return null;
36+
}
37+
38+
Node<E> slow = head;
39+
Node<E> fast = head;
40+
41+
while (fast != null && fast.next != null) {
42+
slow = slow.next;
43+
fast = fast.next.next;
44+
}
45+
46+
return slow.value;
47+
}
48+
49+
@Override
50+
public String toString() {
51+
StringBuilder sb = new StringBuilder("[");
52+
Node<E> current = head;
53+
while (current != null) {
54+
sb.append(current.value);
55+
if (current.next != null) {
56+
sb.append(", ");
57+
}
58+
current = current.next;
59+
}
60+
sb.append("]");
61+
return sb.toString();
62+
}
63+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package com.thealgorithms.datastructures.lists;
2+
3+
import static org.junit.jupiter.api.Assertions.assertEquals;
4+
import static org.junit.jupiter.api.Assertions.assertNull;
5+
6+
import org.junit.jupiter.api.Test;
7+
8+
class TortoiseHareAlgoTest {
9+
10+
@Test
11+
void testAppendAndToString() {
12+
TortoiseHareAlgo<Integer> list = new TortoiseHareAlgo<>();
13+
list.append(10);
14+
list.append(20);
15+
list.append(30);
16+
assertEquals("[10, 20, 30]", list.toString());
17+
}
18+
19+
@Test
20+
void testGetMiddleOdd() {
21+
TortoiseHareAlgo<Integer> list = new TortoiseHareAlgo<>();
22+
list.append(1);
23+
list.append(2);
24+
list.append(3);
25+
list.append(4);
26+
list.append(5);
27+
assertEquals(3, list.getMiddle());
28+
}
29+
30+
@Test
31+
void testGetMiddleEven() {
32+
TortoiseHareAlgo<Integer> list = new TortoiseHareAlgo<>();
33+
list.append(1);
34+
list.append(2);
35+
list.append(3);
36+
list.append(4);
37+
assertEquals(3, list.getMiddle()); // returns second middle
38+
}
39+
40+
@Test
41+
void testEmptyList() {
42+
TortoiseHareAlgo<Integer> list = new TortoiseHareAlgo<>();
43+
assertNull(list.getMiddle());
44+
assertEquals("[]", list.toString());
45+
}
46+
}

0 commit comments

Comments
 (0)