Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
218 commits
Select commit Hold shift + click to select a range
beb0116
fix spring config
Doha2012 Aug 3, 2017
9c74046
fix spring config
Doha2012 Aug 5, 2017
2ca9277
Merge remote-tracking branch 'eugenp/master'
Doha2012 Aug 5, 2017
af812ec
fix spring config
Doha2012 Aug 7, 2017
c4141ab
Merge remote-tracking branch 'eugenp/master'
Doha2012 Aug 7, 2017
df746c6
minor fix
Doha2012 Aug 9, 2017
069d7a3
Merge remote-tracking branch 'eugenp/master'
Doha2012 Aug 9, 2017
5e4c913
Merge remote-tracking branch 'eugenp/master'
Doha2012 Aug 10, 2017
1053180
fix spring-boot module
Doha2012 Aug 11, 2017
99bad7d
Merge remote-tracking branch 'eugenp/master'
Doha2012 Aug 11, 2017
fcbfecf
fix pom
Doha2012 Aug 11, 2017
08eb127
Merge remote-tracking branch 'eugenp/master'
Doha2012 Aug 19, 2017
b47a367
upgrade jackson
Doha2012 Aug 19, 2017
8fa09b2
minor fix
Doha2012 Aug 20, 2017
d76aa38
Merge remote-tracking branch 'eugenp/master'
Doha2012 Aug 20, 2017
18b1334
move reladomo ex to librarie-data (#2485)
lor6 Aug 23, 2017
b6674f6
BAEL-1070 - CharSequence vs String in Java (#2451)
ahmetcetin39 Aug 23, 2017
621d0d2
BAEL-1073 Converting a List to String in Java (#2489)
yasin3061 Aug 23, 2017
15666e8
Binary Search Algorithm (#2452)
techPackets Aug 23, 2017
3f1d94f
Merge remote-tracking branch 'eugenp/master'
Doha2012 Aug 23, 2017
74e67d6
Fix the test method (#2491)
buddhini81 Aug 24, 2017
1cba1b0
minor formatting cleanup
Aug 24, 2017
b00a9e6
formatting work
Aug 24, 2017
154b59f
Updated Example Code for Vavr Collections (#2472)
SeunMatt Aug 24, 2017
9b563d8
Added another remove example (#2484)
nbosecker Aug 24, 2017
c31fc8d
BAEL-1070 - CharSequence vs String in Java (Fixed Test Method) (#2499)
ahmetcetin39 Aug 25, 2017
c12ba86
BAEL-1070 CharSequence vs String in Java (Test case fixed) (#2500)
ahmetcetin39 Aug 25, 2017
c0b64d4
java concurrency
Doha2012 Aug 25, 2017
f4f8be1
cleanup
Doha2012 Aug 25, 2017
29d6807
Merge remote-tracking branch 'eugenp/master'
Doha2012 Aug 25, 2017
8180274
BAEL-1074 Difference between two dates in Java (#2493)
Aug 25, 2017
cec17bd
Updated Example Code for Apache Shiro (#2501)
SeunMatt Aug 25, 2017
d492c6f
BAEL-1014 [Spring MVC with Kotlin] (#2495)
Euniceadu Aug 25, 2017
09d10ac
BAEL-1076 How to get a number of digits in an int? (#2457)
ramansahasi Aug 25, 2017
25263f1
java concurrency (#2502)
Doha2012 Aug 27, 2017
0b85b0a
functional bean registration test (#2503)
lor6 Aug 27, 2017
515e4ca
Add core-java-concurrency to main pom (#2507)
pivovarit Aug 27, 2017
e1e6550
BAEL-1080 Introduction to Future in Vavr (#2488)
hunghoang Aug 27, 2017
c5d77f4
BAEL-1074 Separate difference on date and datetime (#2511)
Aug 28, 2017
d2b90c6
merge
Doha2012 Aug 28, 2017
c9e275a
fix conflict
Doha2012 Aug 28, 2017
73526dd
BAEL - 971 Code Changes (#2492)
kirangonella Aug 28, 2017
904a740
BAEL-1071 Runnable vs Callable in Java (#2513)
baljeet20 Aug 28, 2017
8da9bee
BAEL-1027: Introduction to GraphQL - initial commit (#2515)
felipeazv Aug 28, 2017
b3a291a
BAEL-1078 Stream Indices - Update pom.xml (#2516)
pedja4 Aug 29, 2017
3c5ed71
BAEL-1075 (#2497)
shekhar-srivastava Aug 29, 2017
5945d5c
BAEL-1072 - Difference between Proxy, Decorator, Adapter, and Bridge …
ramansahasi Aug 29, 2017
7df2635
ejb refactor (#2528)
pivovarit Aug 29, 2017
4c7f165
Bael 776 vert.x and rxjava (#2529)
danidemi Aug 30, 2017
085cef8
string search algorithms article (#2460)
ante003 Aug 30, 2017
2aeef70
BAEL-1074 move difference between dates from core-java to libraries (…
Aug 30, 2017
9044109
java 8
Doha2012 Aug 30, 2017
5a463b3
clean up
Doha2012 Aug 30, 2017
8c43cc2
add core-java-8 to main pom
Doha2012 Aug 30, 2017
9c03b45
move java8 code (#2535)
Doha2012 Aug 31, 2017
2f3e364
Moved article code to libraries, added a null check and reverted chan…
shekhar-srivastava Aug 31, 2017
49acba2
optional examples (#2537)
lor6 Aug 31, 2017
d1a1322
Kotlin idioms (#2534)
pivovarit Aug 31, 2017
78e47f1
RxJava Vertx refactor (#2531)
pivovarit Aug 31, 2017
4b77e37
BAEL-1071 Added new example (#2532)
baljeet20 Aug 31, 2017
89927e4
Minor fix (part 2): move @EnableOAuth2Sso from Application to Config …
jowiho Aug 31, 2017
6d6b245
Refactor (#2538)
pivovarit Aug 31, 2017
12ffbe0
BAEL-1078 Code formatting (#2539)
pedja4 Aug 31, 2017
6d797a2
Refactor JavaDirectoryDelete (#2542)
pivovarit Sep 1, 2017
aaa3d11
Update versions in libraries (#2543)
pivovarit Sep 1, 2017
86c1d6d
First proposition for introduction to spring-yarg (#2540)
hugosama1 Sep 1, 2017
7e26990
An Introduction to Vavr's Validation API - Alejandro Gervasio | aleja…
Sep 1, 2017
498a84c
removed If condition (#2546)
shekhar-srivastava Sep 1, 2017
d635e9a
binary search folder created (#2541)
techPackets Sep 2, 2017
94d02ed
BAEL-1078 How to iterate over a stream with indices (#2544)
fatosmorina Sep 2, 2017
7262427
Version update 9 02 (#2547)
pivovarit Sep 2, 2017
01529c3
sneaky throws (#2505)
alincojanu Sep 2, 2017
3c4faf9
BAEL-1018 - Introduction to MBassador (#2551)
egoebelbecker Sep 2, 2017
159f918
Refactor mustache
pivovarit Sep 3, 2017
b6830cc
Updated Vavr Collections Example Code (#2550)
SeunMatt Sep 3, 2017
4baaf9f
BAEL-1085 (#2428)
Thoughtscript Sep 3, 2017
d8398ca
BAEL-1123 - Testing Linked List for Cycles - deep20jain@gmail.com (#2…
deep20jain Sep 3, 2017
acf9bf7
JDeferred (#2553)
AbhinabKanrar Sep 3, 2017
2b293e5
BAEL-1116 Spring swagger codegen (#2527)
Namozag Sep 4, 2017
839a68f
BAEL-1106 Introduction to javax.measure (#2522)
parthkaria Sep 4, 2017
133bbe1
Introduced jmh benchmarking (#2549)
ramansahasi Sep 4, 2017
1afa7e2
Bael 1043/protonpack (#2415)
daveRanjan Sep 4, 2017
b49ad4e
Merge pull request #2552 from eugenp/Refactor_mustache
slavisa-baeldung Sep 4, 2017
fd37ffb
Retrofit rx (#2519)
Namozag Sep 4, 2017
0d599ae
import source BAEL-1084 (#2560)
chrisoberle Sep 5, 2017
7b836d9
Pairs functionality in Java (Core Java, Apache Commons and Vavr) (#2…
bxnkole Sep 5, 2017
cafdf51
Refactor JDeffered (#2559)
pivovarit Sep 5, 2017
0d9068c
A Guide to RxJava (#2557)
alincojanu Sep 5, 2017
830db34
Refactor RxJava (#2564)
pivovarit Sep 5, 2017
7916882
Refactor stream with indices (#2565)
pivovarit Sep 5, 2017
4b9ec46
unit-api examples (#2566)
lor6 Sep 6, 2017
7904c3e
BAEL-1065 Simple performance check StringBuffer vs StringBuilder. (#2…
iaforek Sep 6, 2017
216f7d8
Reformat pom.xml (#2567)
pivovarit Sep 6, 2017
39fc396
BAEL-982 - Moving to mustache module
slavisa-baeldung Sep 6, 2017
e0c1678
Refactor Retrofit samples (#2568)
pivovarit Sep 6, 2017
218210f
Update Jenkins job link (#2548)
pivovarit Sep 6, 2017
4c73a0c
Update README.md (#2570)
pivovarit Sep 6, 2017
2f1bb7b
Merge pull request #2569 from eugenp/BAEL-982-mustache-spring-boot
slavisa-baeldung Sep 6, 2017
9f10f7c
BAEL-509: Removed Duplicate Ant Matcher (#2558)
Thoughtscript Sep 6, 2017
efbac53
BAEL-973 Apache Commons Commons IO (#2262)
charithdesilva Sep 7, 2017
da32a94
BAEL-1018 - Introduction to MBassador (#2576)
egoebelbecker Sep 7, 2017
4a6d2a6
Update README.md (#2577)
pivovarit Sep 7, 2017
9c753f6
Update README.md (#2578)
pivovarit Sep 7, 2017
e625535
Update README.md (#2579)
pivovarit Sep 7, 2017
f6d6274
Update README.md (#2580)
pivovarit Sep 7, 2017
cc304b0
Update README.md (#2581)
pivovarit Sep 7, 2017
35ac22c
FunctionalJava article (#2586)
nabyla Sep 8, 2017
cccd6a3
BAEL 1123 - deep20jain@gmail.com (#2572)
deep20jain Sep 8, 2017
644ecbd
BAEL-1090 Difference between Compressed String and Compact String in …
mansi2392 Sep 8, 2017
4589860
BAEL-1090 Fixed minor issue with total time calculation (#2589)
pedja4 Sep 8, 2017
f01d1e2
Build optimization (#2591)
pivovarit Sep 9, 2017
2b5a6c1
Build opt (#2593)
pivovarit Sep 9, 2017
aa612b7
BAEL-1109 Introduction to JCache (#2594)
Sep 9, 2017
514996c
Java Beans Validation with Hibernate Validator - BAEL-1138 - Alejandr…
Sep 9, 2017
f6ced9a
Examples for retrofit guide (#2596)
hugosama1 Sep 10, 2017
07d7ec5
Functional Java PR (#2599)
nabyla Sep 10, 2017
d4d0305
Guide to RxJava (#2597)
alincojanu Sep 10, 2017
5f59a1b
BAEL-982 - reusing model
slavisa-baeldung Sep 11, 2017
3e192a0
BAEL-1085: @RequestBody and @ResponseBody Annotation (#2583)
Thoughtscript Sep 11, 2017
f95f0da
Rename JdbcIntegrationTest to JdbcLiveTest (#2600)
pivovarit Sep 11, 2017
19f39c1
Fix int tests (#2604)
pivovarit Sep 11, 2017
de07e1f
BAEL-1058 (#2606)
aietcn Sep 11, 2017
19204f5
Merge pull request #2603 from eugenp/BAEL-982
slavisa-baeldung Sep 11, 2017
57f55e1
Fix int tests (#2605)
pivovarit Sep 11, 2017
bf51567
Fix int tests (#2607)
pivovarit Sep 11, 2017
63678ff
Fix int tests (#2608)
pivovarit Sep 11, 2017
4cba067
Fix int tests (#2609)
pivovarit Sep 11, 2017
22015c8
BAEL-1088 Model, ModelMap and ModelView in Spring MVC (#2590)
ahmetcetin39 Sep 11, 2017
4edcd08
Kotlin jvm field (#2610)
Sep 12, 2017
488eab1
BAEL-890 - Kotlin-Allopen with Spring example (#2582)
Sep 12, 2017
0fd8f99
Fix int tests (#2611)
pivovarit Sep 12, 2017
7d0e9da
Update README.md (#2571)
pivovarit Sep 12, 2017
ba9d832
Refactor RxJava (#2612)
pivovarit Sep 12, 2017
8ac2322
Bael 817 refactor (#2575)
dimitarsazdovski Sep 13, 2017
75c1003
minor cleanup in the spring 5 module
Sep 13, 2017
0f22f7b
formatting work
Sep 13, 2017
624f7f4
Rename ArquillianTest to ArquillianLiveTest (#2615)
pivovarit Sep 13, 2017
7bfc50e
BAEL-1121
Sep 14, 2017
911ca0f
Merge remote-tracking branch 'upstream/master' into earth001_BAEL-1121
Sep 14, 2017
bbe925b
Merge remote-tracking branch 'upstream/master'
Sep 14, 2017
47959c6
delete pom.xml.orig
Sep 14, 2017
c0350cc
BAEL-973: updated README (#2618)
KevinGilmore Sep 14, 2017
8e7fc43
cleanup and refactor per comments (#2601)
chrisoberle Sep 14, 2017
556e8fa
Refactor jcache examples (#2620)
pivovarit Sep 14, 2017
e79294b
Merge pull request #2617 from earth001/master
evandor Sep 14, 2017
1132943
BAEL-1069: Guide to diamond operator in Java (#2587)
Sgitario Sep 15, 2017
231500b
Chnages for BAEL-527 (#2621)
khatwaniNikhil Sep 15, 2017
02c5135
Changes according to remarks (#2624)
dimitarsazdovski Sep 15, 2017
4846177
Geotools refactor (#2626)
pivovarit Sep 16, 2017
f3557de
16.09 (#2628)
cody756 Sep 16, 2017
a3f70aa
Updated Lang3Utils.java (#2584)
kirangonella Sep 16, 2017
f384e46
Example Code for N1QL (#2629)
SeunMatt Sep 17, 2017
9081c08
Add stream supplier test (#2631)
nabyla Sep 17, 2017
1ced2a3
BAEL-1052: EthereumJ (#2614)
Thoughtscript Sep 17, 2017
a323d64
JIRA issue : BAEL-960. Collection Factory Methods for Vavr. User: ign…
Sep 17, 2017
3a96543
BAEL-1072 Difference between Proxy, Decorator, Adapter, and Bridge Pa…
ramansahasi Sep 17, 2017
f7ab9fa
Updated N1QL Example Code (#2636)
SeunMatt Sep 18, 2017
562b9fc
Updated extractJsonResult method (#2639)
SeunMatt Sep 18, 2017
0dd6ddd
Updated test method names to BDD convention (#2643)
daveRanjan Sep 18, 2017
11f5ac7
BAEL-1020 -Introductionjukito (#2634)
hugosama1 Sep 19, 2017
d626f9c
rxjava utility operators test (#2642)
alincojanu Sep 19, 2017
90a102e
rename module to couchbase (#2646)
SeunMatt Sep 19, 2017
60acc2f
linkrest ex (#2647)
lor6 Sep 20, 2017
8aa9a65
Merge remote-tracking branch 'eugenp/master'
Doha2012 Sep 20, 2017
771ded6
BAEL-1069 update README (#2648)
KevinGilmore Sep 20, 2017
3d164d0
rename kotlin to core-kotlin
Doha2012 Sep 20, 2017
01dddf4
BAEL-1088 / fix / RequestMapping is replaced with GetMapping (#2640)
ahmetcetin39 Sep 20, 2017
1852438
rename kotlin to core-kotlin (#2651)
Doha2012 Sep 20, 2017
16b6e08
BAEL-1107 : Introduction to Apache Cayenne Orm (#2650)
Sep 21, 2017
5fe5887
add simpleentry test (#2654)
lor6 Sep 21, 2017
476f0df
author: ignaciogallego@gmail.com article:Collection Factory Methods f…
Sep 21, 2017
c7f180f
BAEL-817 README (#2655)
KevinGilmore Sep 21, 2017
1ddd46f
BAEL-1080 Introduction to Future in Vavr (#2627)
hunghoang Sep 21, 2017
ec6aca9
BAEL-781: Explore the new Spring Cloud Gateway (#2613)
felipeazv Sep 21, 2017
f5c539f
BAEL-1107: formating and version (#2660)
Sep 23, 2017
68d807e
close cache instances properly (#2630)
Sep 23, 2017
dca9403
Refactor rxjava-jdbc (#2663)
Sep 23, 2017
8ed5c8e
tomcat app ex (#2625)
lor6 Sep 23, 2017
8db9460
BAEL-1139 How to Integration with the JIRA API (#2665)
yasin3061 Sep 24, 2017
c9072e5
rxjava schedulers (#2668)
alincojanu Sep 24, 2017
52ac2ba
BAEL-1107: update properties of Author (#2666)
Sep 24, 2017
2beb5f0
BAEL-960 and BAEL-1084 README updates (#2670)
KevinGilmore Sep 24, 2017
8bf825d
Code examples for the articl on Apache bags (#2671)
adamd1985 Sep 25, 2017
c9a754f
cleanup resources
Sep 25, 2017
d10d758
Build optimization 26.09.2017 (#2676)
pivovarit Sep 26, 2017
d79fcae
–BAEL-866 Changes (#2669)
khatwaniNikhil Sep 26, 2017
692fe31
BAEL-815 Introduction to JGraphT (#2653)
parthkaria Sep 27, 2017
9fa1469
Fix/hibernate immutable tests (#2682)
Sep 28, 2017
4948204
testing cleanup
Sep 28, 2017
0fa1689
update to 2.0, formatting (#2688)
lor6 Sep 30, 2017
77aa65b
remove project (#2689)
lor6 Sep 30, 2017
9a70874
Merge remote-tracking branch 'eugenp/master'
Doha2012 Sep 30, 2017
cfb952f
Atomix Intro Project (#2675)
abirkhan04 Sep 30, 2017
4c6e4c1
Update Person class to use property "id" like identifier.
Sep 30, 2017
29cc370
Merge pull request #2694 from earth001/master
evandor Oct 1, 2017
d343c59
BAEL-815 Move JGraphT code from libraries to algorithms (#2695)
parthkaria Oct 1, 2017
3220913
From JDO to XML and Back Again 2/3 (#2681)
shaimaa-hshalaby Oct 1, 2017
7cb94f6
Refactor Atomix samples (#2696)
pivovarit Oct 2, 2017
e820fbb
update spring 5
Doha2012 Oct 2, 2017
dfc0a97
Merge remote-tracking branch 'eugenp/master'
Doha2012 Oct 2, 2017
691f929
cleanup pom
Doha2012 Oct 2, 2017
9aa221a
cleanup spring 5 pom (#2700)
Doha2012 Oct 2, 2017
dcc7fa6
Updated for BAEL 971 (#2702)
kirangonella Oct 3, 2017
8a77746
container elements validation ex (#2707)
lor6 Oct 4, 2017
eaba521
runWith (#2691)
alincojanu Oct 4, 2017
75803f1
BAEL-1157: Apache Cayenne - Advanced Querying (#2708)
Oct 5, 2017
7b618af
Merge remote-tracking branch 'eugenp/master'
Doha2012 Oct 5, 2017
e71902d
BAEL-1139 added more examples (#2686)
YasinBhojawala Oct 5, 2017
4cdcce6
Fix Vavr module (#2709)
pivovarit Oct 5, 2017
6818505
Update README.md (#2637)
jitendrar292 Oct 6, 2017
c521aa6
BAEL-1155 crdt (#2659)
tomekl007 Oct 6, 2017
1dbc2c9
Refactor JiraClient (#2710)
pivovarit Oct 6, 2017
3f21a3b
Removed unintentionally commented code (#2711)
yasin3061 Oct 6, 2017
a4ca964
mockito exceptions
Doha2012 Oct 7, 2017
4653e0a
Merge pull request #2713 from Doha2012/master
lor6 Oct 7, 2017
9e273b1
BAEL-1171 java.lang.String API (#2693)
Oct 8, 2017
26508f3
Docx4j article (#2714)
nabyla Oct 8, 2017
491649a
Refactor Docx examples (#2716)
pivovarit Oct 8, 2017
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,5 @@ SpringDataInjectionDemo/.mvn/wrapper/maven-wrapper.properties
spring-call-getters-using-reflection/.mvn/wrapper/maven-wrapper.properties

spring-check-if-a-property-is-null/.mvn/wrapper/maven-wrapper.properties
*.springBeans

2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,4 @@ Any IDE can be used to work with the projects, but if you're using Eclipse, cons

CI - Jenkins
================================
This tutorials project is being built **[>> HERE](https://rest-security.ci.cloudbees.com/job/tutorials/)**
This tutorials project is being built **[>> HERE](https://rest-security.ci.cloudbees.com/job/github%20projects%20Jobs/job/tutorials/)**
1 change: 1 addition & 0 deletions algorithms/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@
- [Check If a Number Is Prime in Java](http://www.baeldung.com/java-prime-numbers)
- [Example of Hill Climbing Algorithm](http://www.baeldung.com/java-hill-climbing-algorithm)
- [Monte Carlo Tree Search for Tic-Tac-Toe Game](http://www.baeldung.com/java-monte-carlo-tree-search)
- [String Search Algorithms for Large Texts](http://www.baeldung.com/java-full-text-search-algorithms)
5 changes: 5 additions & 0 deletions algorithms/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,11 @@
<artifactId>jenetics</artifactId>
<version>3.7.0</version>
</dependency>
<dependency>
<groupId>org.jgrapht</groupId>
<artifactId>jgrapht-core</artifactId>
<version>1.0.1</version>
</dependency>
</dependencies>

<build>
Expand Down
26 changes: 0 additions & 26 deletions algorithms/src/main/java/com/baeldung/algorithms/BinarySearch.java

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package com.baeldung.algorithms.binarysearch;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;

public class BinarySearch {

public int runBinarySearchIteratively(int[] sortedArray, int key, int low, int high) {

int index = Integer.MAX_VALUE;

while (low <= high) {

int mid = (low + high) / 2;

if (sortedArray[mid] < key) {
low = mid + 1;
} else if (sortedArray[mid] > key) {
high = mid - 1;
} else if (sortedArray[mid] == key) {
index = mid;
break;
}
}
return index;
}

public int runBinarySearchRecursively(int[] sortedArray, int key, int low, int high) {

int middle = (low + high) / 2;
if (high < low) {
return -1;
}

if (key == sortedArray[middle]) {
return middle;
} else if (key < sortedArray[middle]) {
return runBinarySearchRecursively(sortedArray, key, low, middle - 1);
} else {
return runBinarySearchRecursively(sortedArray, key, middle + 1, high);
}
}

public int runBinarySearchUsingJavaArrays(int[] sortedArray, Integer key) {
int index = Arrays.binarySearch(sortedArray, key);
return index;
}

public int runBinarySearchUsingJavaCollections(List<Integer> sortedList, Integer key) {
int index = Collections.binarySearch(sortedList, key);
return index;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package com.baeldung.algorithms.linkedlist;

public class CycleDetectionBruteForce {

public static <T> CycleDetectionResult<T> detectCycle(Node<T> head) {
if (head == null) {
return new CycleDetectionResult<>(false, null);
}

Node<T> it1 = head;
int nodesTraversedByOuter = 0;
while (it1 != null && it1.next != null) {
it1 = it1.next;
nodesTraversedByOuter++;

int x = nodesTraversedByOuter;
Node<T> it2 = head;
int noOfTimesCurrentNodeVisited = 0;

while (x > 0) {
it2 = it2.next;

if (it2 == it1) {
noOfTimesCurrentNodeVisited++;
}

if (noOfTimesCurrentNodeVisited == 2) {
return new CycleDetectionResult<>(true, it1);
}

x--;
}
}

return new CycleDetectionResult<>(false, null);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package com.baeldung.algorithms.linkedlist;

public class CycleDetectionByFastAndSlowIterators {

public static <T> CycleDetectionResult<T> detectCycle(Node<T> head) {
if (head == null) {
return new CycleDetectionResult<>(false, null);
}

Node<T> slow = head;
Node<T> fast = head;

while (fast != null && fast.next != null) {
slow = slow.next;
fast = fast.next.next;

if (slow == fast) {
return new CycleDetectionResult<>(true, fast);
}
}

return new CycleDetectionResult<>(false, null);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package com.baeldung.algorithms.linkedlist;

import java.util.HashSet;
import java.util.Set;

public class CycleDetectionByHashing {

public static <T> CycleDetectionResult<T> detectCycle(Node<T> head) {
if (head == null) {
return new CycleDetectionResult<>(false, null);
}

Set<Node<T>> set = new HashSet<>();
Node<T> node = head;

while (node != null) {
if (set.contains(node)) {
return new CycleDetectionResult<>(true, node);
}
set.add(node);
node = node.next;
}

return new CycleDetectionResult<>(false, null);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.baeldung.algorithms.linkedlist;

public class CycleDetectionResult<T> {
boolean cycleExists;
Node<T> node;

public CycleDetectionResult(boolean cycleExists, Node<T> node) {
super();
this.cycleExists = cycleExists;
this.node = node;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package com.baeldung.algorithms.linkedlist;

public class CycleRemovalBruteForce {

public static <T> boolean detectAndRemoveCycle(Node<T> head) {
CycleDetectionResult<T> result = CycleDetectionByFastAndSlowIterators.detectCycle(head);

if (result.cycleExists) {
removeCycle(result.node, head);
}

return result.cycleExists;
}

/**
* @param loopNodeParam - reference to the node where Flyods cycle
* finding algorithm ends, i.e. the fast and the slow iterators
* meet.
* @param head - reference to the head of the list
*/
private static <T> void removeCycle(Node<T> loopNodeParam, Node<T> head) {
Node<T> it = head;

while (it != null) {
if (isNodeReachableFromLoopNode(it, loopNodeParam)) {
Node<T> loopStart = it;
findEndNodeAndBreakCycle(loopStart);
break;
}
it = it.next;
}
}

private static <T> boolean isNodeReachableFromLoopNode(Node<T> it, Node<T> loopNodeParam) {
Node<T> loopNode = loopNodeParam;

do {
if (it == loopNode) {
return true;
}
loopNode = loopNode.next;
} while (loopNode.next != loopNodeParam);

return false;
}

private static <T> void findEndNodeAndBreakCycle(Node<T> loopStartParam) {
Node<T> loopStart = loopStartParam;

while (loopStart.next != loopStartParam) {
loopStart = loopStart.next;
}

loopStart.next = null;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package com.baeldung.algorithms.linkedlist;

public class CycleRemovalByCountingLoopNodes {

public static <T> boolean detectAndRemoveCycle(Node<T> head) {
CycleDetectionResult<T> result = CycleDetectionByFastAndSlowIterators.detectCycle(head);

if (result.cycleExists) {
removeCycle(result.node, head);
}

return result.cycleExists;
}

private static <T> void removeCycle(Node<T> loopNodeParam, Node<T> head) {
int cycleLength = calculateCycleLength(loopNodeParam);
Node<T> cycleLengthAdvancedIterator = head;
Node<T> it = head;

for (int i = 0; i < cycleLength; i++) {
cycleLengthAdvancedIterator = cycleLengthAdvancedIterator.next;
}

while (it.next != cycleLengthAdvancedIterator.next) {
it = it.next;
cycleLengthAdvancedIterator = cycleLengthAdvancedIterator.next;
}

cycleLengthAdvancedIterator.next = null;
}

private static <T> int calculateCycleLength(Node<T> loopNodeParam) {
Node<T> loopNode = loopNodeParam;
int length = 1;

while (loopNode.next != loopNodeParam) {
length++;
loopNode = loopNode.next;
}

return length;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package com.baeldung.algorithms.linkedlist;

public class CycleRemovalWithoutCountingLoopNodes {

public static <T> boolean detectAndRemoveCycle(Node<T> head) {
CycleDetectionResult<T> result = CycleDetectionByFastAndSlowIterators.detectCycle(head);

if (result.cycleExists) {
removeCycle(result.node, head);
}

return result.cycleExists;
}

private static <T> void removeCycle(Node<T> meetingPointParam, Node<T> head) {
Node<T> loopNode = meetingPointParam;
Node<T> it = head;

while (loopNode.next != it.next) {
it = it.next;
loopNode = loopNode.next;
}

loopNode.next = null;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package com.baeldung.algorithms.linkedlist;

public class Node<T> {
T data;
Node<T> next;

public static <T> Node<T> createNewNode(T data, Node<T> next) {
Node<T> node = new Node<T>();
node.data = data;
node.next = next;
return node;
}

public static <T> void traverseList(Node<T> root) {
if (root == null) {
return;
}

Node<T> node = root;
while (node != null) {
System.out.println(node.data);
node = node.next;
}
}

public static <T> Node<T> getTail(Node<T> root) {
if (root == null) {
return null;
}

Node<T> node = root;
while (node.next != null) {
node = node.next;
}
return node;
}

}
Loading