Skip to content

Commit 0f197fa

Browse files
author
Koushik Sai
committed
Add algorithm to find middle of singly linked list with tests
1 parent d9f2ac8 commit 0f197fa

File tree

2 files changed

+88
-0
lines changed

2 files changed

+88
-0
lines changed
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package com.thealgorithms.datastructures.lists;
2+
3+
public final class FindMiddleOfSinglyLinkedList {
4+
5+
static final class Node<E> {
6+
public Node<E> next;
7+
public E value;
8+
9+
Node(E value, Node<E> next) {
10+
this.value = value;
11+
this.next = next;
12+
}
13+
}
14+
15+
/**
16+
* Finds the middle node of a singly linked list.
17+
*
18+
* @param head the head node of the singly linked list
19+
* @param <E> the type of elements in the linked list
20+
* @return the middle node of the linked list; if the list is empty, returns null
21+
*/
22+
public static <E> Node<E> findMiddle(Node<E> head) {
23+
if (head == null) {
24+
return null; // Empty list
25+
}
26+
27+
Node<E> slowPointer = head;
28+
Node<E> fastPointer = head;
29+
30+
// Move fastPointer two steps and slowPointer one step at a time
31+
while (fastPointer != null && fastPointer.next != null) {
32+
slowPointer = slowPointer.next;
33+
fastPointer = fastPointer.next.next;
34+
}
35+
36+
// When fastPointer reaches the end, slowPointer will be at the middle
37+
return slowPointer;
38+
}
39+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
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+
final class FindMiddleOfSinglyLinkedListTest {
9+
10+
@Test
11+
void testFindMiddleEmptyList() {
12+
FindMiddleOfSinglyLinkedList.Node<Integer> head = null;
13+
var middle = FindMiddleOfSinglyLinkedList.findMiddle(head);
14+
assertNull(middle, "Middle of an empty list should be null.");
15+
}
16+
17+
@Test
18+
void testFindMiddleSingleElement() {
19+
var head = new FindMiddleOfSinglyLinkedList.Node<>(1, null);
20+
var middle = FindMiddleOfSinglyLinkedList.findMiddle(head);
21+
assertEquals(1, middle.value,
22+
"Middle of a single-element list should be the only element.");
23+
}
24+
25+
@Test
26+
void testFindMiddleOddElements() {
27+
var head = new FindMiddleOfSinglyLinkedList.Node<>(1,
28+
new FindMiddleOfSinglyLinkedList.Node<>(2,
29+
new FindMiddleOfSinglyLinkedList.Node<>(3,
30+
new FindMiddleOfSinglyLinkedList.Node<>(4,
31+
new FindMiddleOfSinglyLinkedList.Node<>(5, null)))));
32+
33+
var middle = FindMiddleOfSinglyLinkedList.findMiddle(head);
34+
assertEquals(3, middle.value,
35+
"Middle of a 5-element list should be the 3rd element.");
36+
}
37+
38+
@Test
39+
void testFindMiddleEvenElements() {
40+
var head = new FindMiddleOfSinglyLinkedList.Node<>(1,
41+
new FindMiddleOfSinglyLinkedList.Node<>(2,
42+
new FindMiddleOfSinglyLinkedList.Node<>(3,
43+
new FindMiddleOfSinglyLinkedList.Node<>(4, null))));
44+
45+
var middle = FindMiddleOfSinglyLinkedList.findMiddle(head);
46+
assertEquals(3, middle.value,
47+
"Middle of a 4-element list should be the 3rd element.");
48+
}
49+
}

0 commit comments

Comments
 (0)