Skip to content

Commit c58d925

Browse files
committed
Add FindMiddleNode algorithm with ListNode class and JUnit tests
1 parent 5033bd5 commit c58d925

File tree

2 files changed

+7
-5
lines changed

2 files changed

+7
-5
lines changed

src/main/java/com/thealgorithms/datastructures/lists/FindMiddleNode.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
package com.thealgorithms.datastructures.lists;
22

3-
public class FindMiddleNode {
3+
public final class FindMiddleNode {
4+
5+
private FindMiddleNode() {
6+
throw new UnsupportedOperationException("Utility class");
7+
}
48

59
public static ListNode findMiddle(ListNode head) {
610
ListNode slow = head;
@@ -24,4 +28,3 @@ public static void main(String[] args) {
2428
System.out.println("Middle node value: " + middle.value);
2529
}
2630
}
27-
// package com.thealgorithms.datastructures.lists;

src/test/java/com/thealgorithms/datastructures/lists/FindMiddleNodeTest.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,13 @@ public class FindMiddleNodeTest {
77

88
@Test
99
public void testOddLengthList() {
10-
ListNode head = createList(new int[]{1, 2, 3, 4, 5});
10+
ListNode head = createList(new int[] { 1, 2, 3, 4, 5 });
1111
assertEquals(3, FindMiddleNode.findMiddle(head).value);
1212
}
1313

1414
@Test
1515
public void testEvenLengthList() {
16-
ListNode head = createList(new int[]{1, 2, 3, 4, 5, 6});
16+
ListNode head = createList(new int[] { 1, 2, 3, 4, 5, 6 });
1717
assertEquals(4, FindMiddleNode.findMiddle(head).value);
1818
}
1919

@@ -27,4 +27,3 @@ private ListNode createList(int[] values) {
2727
return head;
2828
}
2929
}
30-

0 commit comments

Comments
 (0)