mirror of
https://github.com/TheAlgorithms/Java.git
synced 2025-12-19 07:00:35 +08:00
Add TortoiseHareAlgo implementation with append, toString, and getMiddle methods (#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>
This commit is contained in:
@@ -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.
|
||||
|
||||
@@ -0,0 +1,63 @@
|
||||
package com.thealgorithms.datastructures.lists;
|
||||
|
||||
public class TortoiseHareAlgo<E> {
|
||||
static final class Node<E> {
|
||||
Node<E> next;
|
||||
E value;
|
||||
|
||||
private Node(E value, Node<E> next) {
|
||||
this.value = value;
|
||||
this.next = next;
|
||||
}
|
||||
}
|
||||
|
||||
private Node<E> head = null;
|
||||
|
||||
public TortoiseHareAlgo() {
|
||||
head = null;
|
||||
}
|
||||
|
||||
public void append(E value) {
|
||||
Node<E> newNode = new Node<>(value, null);
|
||||
if (head == null) {
|
||||
head = newNode;
|
||||
return;
|
||||
}
|
||||
Node<E> current = head;
|
||||
while (current.next != null) {
|
||||
current = current.next;
|
||||
}
|
||||
current.next = newNode;
|
||||
}
|
||||
|
||||
public E getMiddle() {
|
||||
if (head == null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
Node<E> slow = head;
|
||||
Node<E> 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<E> current = head;
|
||||
while (current != null) {
|
||||
sb.append(current.value);
|
||||
if (current.next != null) {
|
||||
sb.append(", ");
|
||||
}
|
||||
current = current.next;
|
||||
}
|
||||
sb.append("]");
|
||||
return sb.toString();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,46 @@
|
||||
package com.thealgorithms.datastructures.lists;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertNull;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
class TortoiseHareAlgoTest {
|
||||
|
||||
@Test
|
||||
void testAppendAndToString() {
|
||||
TortoiseHareAlgo<Integer> list = new TortoiseHareAlgo<>();
|
||||
list.append(10);
|
||||
list.append(20);
|
||||
list.append(30);
|
||||
assertEquals("[10, 20, 30]", list.toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
void testGetMiddleOdd() {
|
||||
TortoiseHareAlgo<Integer> 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<Integer> 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<Integer> list = new TortoiseHareAlgo<>();
|
||||
assertNull(list.getMiddle());
|
||||
assertEquals("[]", list.toString());
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user