BAEL-1773 Find the middle element of a Linked List (#4463)
* BAEL-1773 - find middle element of linked list * changes from review * changes from review * find middle element in linked list * typo * changes from CR * BAEL-1773 formatting
This commit is contained in:
parent
b62c35bd7a
commit
2a07b03b69
@ -1,58 +0,0 @@
|
||||
package com.baeldung.linkedlist;
|
||||
|
||||
/**
|
||||
* Implementation of a singly linked list.
|
||||
*/
|
||||
public class LinkedList {
|
||||
private Node head;
|
||||
private Node tail;
|
||||
|
||||
public Node head() {
|
||||
return head;
|
||||
}
|
||||
|
||||
public void add(String data) {
|
||||
Node newNode = new Node(data);
|
||||
|
||||
if (head == null) {
|
||||
head = newNode;
|
||||
tail = newNode;
|
||||
} else {
|
||||
tail.next = newNode;
|
||||
tail = newNode;
|
||||
}
|
||||
}
|
||||
|
||||
public static class Node {
|
||||
private Node next;
|
||||
private String data;
|
||||
|
||||
public Node(String data) {
|
||||
this.data = data;
|
||||
}
|
||||
|
||||
public String data() {
|
||||
return data;
|
||||
}
|
||||
|
||||
public void setData(String data) {
|
||||
this.data = data;
|
||||
}
|
||||
|
||||
public boolean hasNext() {
|
||||
return next != null;
|
||||
}
|
||||
|
||||
public Node next() {
|
||||
return next;
|
||||
}
|
||||
|
||||
public void setNext(Node next) {
|
||||
this.next = next;
|
||||
}
|
||||
|
||||
public String toString() {
|
||||
return this.data;
|
||||
}
|
||||
}
|
||||
}
|
@ -1,22 +1,23 @@
|
||||
package com.baeldung.linkedlist;
|
||||
|
||||
import java.util.LinkedList;
|
||||
import java.util.Optional;
|
||||
|
||||
import com.baeldung.linkedlist.Node;
|
||||
|
||||
public class MiddleElementLookup {
|
||||
|
||||
public static String findMiddleElementLinkedList(LinkedList<String> linkedList) {
|
||||
public static Optional<String> findMiddleElementLinkedList(LinkedList<String> linkedList) {
|
||||
if (linkedList == null || linkedList.isEmpty()) {
|
||||
return null;
|
||||
return Optional.empty();
|
||||
}
|
||||
|
||||
return linkedList.get((linkedList.size() - 1) / 2);
|
||||
return Optional.ofNullable(linkedList.get((linkedList.size() - 1) / 2));
|
||||
}
|
||||
|
||||
public static String findMiddleElementFromHead(Node head) {
|
||||
public static Optional<String> findMiddleElementFromHead(Node head) {
|
||||
if (head == null) {
|
||||
return null;
|
||||
return Optional.empty();
|
||||
}
|
||||
|
||||
// calculate the size of the list
|
||||
@ -33,17 +34,17 @@ public class MiddleElementLookup {
|
||||
current = current.next();
|
||||
}
|
||||
|
||||
return current.data();
|
||||
return Optional.ofNullable(current.data());
|
||||
}
|
||||
|
||||
public static String findMiddleElementFromHead1PassRecursively(Node head) {
|
||||
public static Optional<String> findMiddleElementFromHead1PassRecursively(Node head) {
|
||||
if (head == null) {
|
||||
return null;
|
||||
return Optional.empty();
|
||||
}
|
||||
|
||||
MiddleAuxRecursion middleAux = new MiddleAuxRecursion();
|
||||
findMiddleRecursively(head, middleAux);
|
||||
return middleAux.middle.data();
|
||||
return Optional.ofNullable(middleAux.middle.data());
|
||||
}
|
||||
|
||||
private static void findMiddleRecursively(Node node, MiddleAuxRecursion middleAux) {
|
||||
@ -63,9 +64,9 @@ public class MiddleElementLookup {
|
||||
middleAux.length--;
|
||||
}
|
||||
|
||||
public static String findMiddleElementFromHead1PassIteratively(Node head) {
|
||||
public static Optional<String> findMiddleElementFromHead1PassIteratively(Node head) {
|
||||
if (head == null) {
|
||||
return null;
|
||||
return Optional.empty();
|
||||
}
|
||||
|
||||
Node slowPointer = head;
|
||||
@ -78,7 +79,7 @@ public class MiddleElementLookup {
|
||||
slowPointer = slowPointer.next();
|
||||
}
|
||||
|
||||
return slowPointer.data();
|
||||
return Optional.ofNullable(slowPointer.data());
|
||||
}
|
||||
|
||||
private static class MiddleAuxRecursion {
|
||||
|
@ -1,6 +1,7 @@
|
||||
package com.baeldung.linkedlist;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertFalse;
|
||||
|
||||
import java.util.LinkedList;
|
||||
|
||||
@ -10,34 +11,83 @@ public class MiddleElementLookupUnitTest {
|
||||
|
||||
@Test
|
||||
public void whenFindingMiddleLinkedList_thenMiddleFound() {
|
||||
assertEquals("3", MiddleElementLookup.findMiddleElementLinkedList(createLinkedList(5)));
|
||||
assertEquals("2", MiddleElementLookup.findMiddleElementLinkedList(createLinkedList(4)));
|
||||
assertEquals("3", MiddleElementLookup
|
||||
.findMiddleElementLinkedList(createLinkedList(5))
|
||||
.get());
|
||||
assertEquals("2", MiddleElementLookup
|
||||
.findMiddleElementLinkedList(createLinkedList(4))
|
||||
.get());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenFindingMiddleFromHead_thenMiddleFound() {
|
||||
assertEquals("3", MiddleElementLookup.findMiddleElementFromHead(createNodesList(5)));
|
||||
assertEquals("2", MiddleElementLookup.findMiddleElementFromHead(createNodesList(4)));
|
||||
assertEquals("3", MiddleElementLookup
|
||||
.findMiddleElementFromHead(createNodesList(5))
|
||||
.get());
|
||||
assertEquals("2", MiddleElementLookup
|
||||
.findMiddleElementFromHead(createNodesList(4))
|
||||
.get());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenFindingMiddleFromHead1PassRecursively_thenMiddleFound() {
|
||||
assertEquals("3", MiddleElementLookup.findMiddleElementFromHead1PassRecursively(createNodesList(5)));
|
||||
assertEquals("2", MiddleElementLookup.findMiddleElementFromHead1PassRecursively(createNodesList(4)));
|
||||
assertEquals("3", MiddleElementLookup
|
||||
.findMiddleElementFromHead1PassRecursively(createNodesList(5))
|
||||
.get());
|
||||
assertEquals("2", MiddleElementLookup
|
||||
.findMiddleElementFromHead1PassRecursively(createNodesList(4))
|
||||
.get());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenFindingMiddleFromHead1PassIteratively_thenMiddleFound() {
|
||||
assertEquals("3", MiddleElementLookup.findMiddleElementFromHead1PassIteratively(createNodesList(5)));
|
||||
assertEquals("2", MiddleElementLookup.findMiddleElementFromHead1PassIteratively(createNodesList(4)));
|
||||
assertEquals("3", MiddleElementLookup
|
||||
.findMiddleElementFromHead1PassIteratively(createNodesList(5))
|
||||
.get());
|
||||
assertEquals("2", MiddleElementLookup
|
||||
.findMiddleElementFromHead1PassIteratively(createNodesList(4))
|
||||
.get());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenListEmptyOrNull_thenMiddleNull() {
|
||||
assertEquals(null, MiddleElementLookup.findMiddleElementLinkedList(null));
|
||||
assertEquals(null, MiddleElementLookup.findMiddleElementFromHead(null));
|
||||
assertEquals(null, MiddleElementLookup.findMiddleElementFromHead1PassIteratively(null));
|
||||
assertEquals(null, MiddleElementLookup.findMiddleElementFromHead1PassRecursively(null));
|
||||
public void whenListEmptyOrNull_thenMiddleNotFound() {
|
||||
// null list
|
||||
assertFalse(MiddleElementLookup
|
||||
.findMiddleElementLinkedList(null)
|
||||
.isPresent());
|
||||
assertFalse(MiddleElementLookup
|
||||
.findMiddleElementFromHead(null)
|
||||
.isPresent());
|
||||
assertFalse(MiddleElementLookup
|
||||
.findMiddleElementFromHead1PassIteratively(null)
|
||||
.isPresent());
|
||||
assertFalse(MiddleElementLookup
|
||||
.findMiddleElementFromHead1PassRecursively(null)
|
||||
.isPresent());
|
||||
|
||||
// empty LinkedList
|
||||
assertFalse(MiddleElementLookup
|
||||
.findMiddleElementLinkedList(new LinkedList<>())
|
||||
.isPresent());
|
||||
|
||||
// LinkedList with nulls
|
||||
LinkedList<String> nullsList = new LinkedList<>();
|
||||
nullsList.add(null);
|
||||
nullsList.add(null);
|
||||
assertFalse(MiddleElementLookup
|
||||
.findMiddleElementLinkedList(nullsList)
|
||||
.isPresent());
|
||||
|
||||
// nodes with null values
|
||||
assertFalse(MiddleElementLookup
|
||||
.findMiddleElementFromHead(new Node(null))
|
||||
.isPresent());
|
||||
assertFalse(MiddleElementLookup
|
||||
.findMiddleElementFromHead1PassIteratively(new Node(null))
|
||||
.isPresent());
|
||||
assertFalse(MiddleElementLookup
|
||||
.findMiddleElementFromHead1PassRecursively(new Node(null))
|
||||
.isPresent());
|
||||
}
|
||||
|
||||
private static LinkedList<String> createLinkedList(int n) {
|
||||
|
Loading…
x
Reference in New Issue
Block a user