Skip to content

Commit 260e805

Browse files
marcos-lgpedja4
authored andcommitted
BAEL-1773 Find the middle element of a Linked List. (eugenp#4350)
* BAEL-1773 - find middle element of linked list * changes from review * changes from review
1 parent 14a1b90 commit 260e805

3 files changed

Lines changed: 195 additions & 0 deletions

File tree

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package com.baeldung.linkedlist;
2+
3+
/**
4+
* Implementation of a singly linked list.
5+
*/
6+
public class LinkedList {
7+
private Node head;
8+
private Node tail;
9+
10+
public Node head() {
11+
return head;
12+
}
13+
14+
public void add(String data) {
15+
Node newNode = new Node(data);
16+
17+
if (head == null) {
18+
head = newNode;
19+
tail = newNode;
20+
} else {
21+
tail.next = newNode;
22+
tail = newNode;
23+
}
24+
}
25+
26+
public static class Node {
27+
private Node next;
28+
private String data;
29+
30+
public Node(String data) {
31+
this.data = data;
32+
}
33+
34+
public String data() {
35+
return data;
36+
}
37+
38+
public void setData(String data) {
39+
this.data = data;
40+
}
41+
42+
public boolean hasNext() {
43+
return next != null;
44+
}
45+
46+
public Node next() {
47+
return next;
48+
}
49+
50+
public void setNext(Node next) {
51+
this.next = next;
52+
}
53+
54+
public String toString() {
55+
return this.data;
56+
}
57+
}
58+
}
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
package com.baeldung.linkedlist;
2+
3+
import com.baeldung.linkedlist.LinkedList.Node;
4+
5+
public class MiddleElementLookup {
6+
7+
public static String findMiddleElement(Node head) {
8+
if (head == null) {
9+
return null;
10+
}
11+
12+
// calculate the size of the list
13+
Node current = head;
14+
int size = 1;
15+
while (current.hasNext()) {
16+
current = current.next();
17+
size++;
18+
}
19+
20+
// iterate till the middle element
21+
current = head;
22+
for (int i = 0; i < (size - 1) / 2; i++) {
23+
current = current.next();
24+
}
25+
26+
return current.data();
27+
}
28+
29+
public static String findMiddleElement1PassRecursively(Node head) {
30+
if (head == null) {
31+
return null;
32+
}
33+
34+
MiddleAuxRecursion middleAux = new MiddleAuxRecursion();
35+
findMiddleRecursively(head, middleAux);
36+
return middleAux.middle.data();
37+
}
38+
39+
private static void findMiddleRecursively(Node node, MiddleAuxRecursion middleAux) {
40+
if (node == null) {
41+
// reached the end
42+
middleAux.length = middleAux.length / 2;
43+
return;
44+
}
45+
middleAux.length++;
46+
findMiddleRecursively(node.next(), middleAux);
47+
48+
if (middleAux.length == 0) {
49+
// found the middle
50+
middleAux.middle = node;
51+
}
52+
53+
middleAux.length--;
54+
}
55+
56+
public static String findMiddleElement1PassIteratively(Node head) {
57+
if (head == null) {
58+
return null;
59+
}
60+
61+
Node slowPointer = head;
62+
Node fastPointer = head;
63+
64+
while (fastPointer.hasNext() && fastPointer.next()
65+
.hasNext()) {
66+
fastPointer = fastPointer.next()
67+
.next();
68+
slowPointer = slowPointer.next();
69+
}
70+
71+
return slowPointer.data();
72+
}
73+
74+
private static class MiddleAuxRecursion {
75+
Node middle;
76+
int length = 0;
77+
}
78+
79+
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package com.baeldung.linkedlist;
2+
3+
import static org.junit.Assert.assertEquals;
4+
5+
import org.junit.Test;
6+
7+
public class MiddleElementLookupUnitTest {
8+
9+
@Test
10+
public void whenFindingMiddle_thenMiddleFound() {
11+
String middle = MiddleElementLookup.findMiddleElement(createList(5).head());
12+
assertEquals("3", middle);
13+
14+
middle = MiddleElementLookup.findMiddleElement(createList(4).head());
15+
assertEquals("2", middle);
16+
}
17+
18+
@Test
19+
public void whenFindingMiddle1PassRecursively_thenMiddleFound() {
20+
String middle = MiddleElementLookup.findMiddleElement1PassRecursively(createList(5).head());
21+
assertEquals("3", middle);
22+
23+
middle = MiddleElementLookup.findMiddleElement1PassRecursively(createList(4).head());
24+
assertEquals("2", middle);
25+
}
26+
27+
@Test
28+
public void whenFindingMiddle1PassIteratively_thenMiddleFound() {
29+
String middle = MiddleElementLookup.findMiddleElement1PassIteratively(createList(5).head());
30+
assertEquals("3", middle);
31+
32+
middle = MiddleElementLookup.findMiddleElement1PassIteratively(createList(4).head());
33+
assertEquals("2", middle);
34+
}
35+
36+
@Test
37+
public void whenListEmptyOrNull_thenMiddleNull() {
38+
String middle = MiddleElementLookup.findMiddleElement(null);
39+
assertEquals(null, middle);
40+
41+
middle = MiddleElementLookup.findMiddleElement1PassIteratively(null);
42+
assertEquals(null, middle);
43+
44+
middle = MiddleElementLookup.findMiddleElement1PassRecursively(null);
45+
assertEquals(null, middle);
46+
}
47+
48+
private static LinkedList createList(int n) {
49+
LinkedList list = new LinkedList();
50+
51+
for (int i = 1; i <= n; i++) {
52+
list.add(String.valueOf(i));
53+
}
54+
55+
return list;
56+
}
57+
58+
}

0 commit comments

Comments
 (0)