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:
committed by
Predrag Maric
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 {
|
||||
|
||||
Reference in New Issue
Block a user