-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCycleInLinkedList.java
More file actions
executable file
·55 lines (47 loc) · 1.46 KB
/
Copy pathCycleInLinkedList.java
File metadata and controls
executable file
·55 lines (47 loc) · 1.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
///usr/bin/env jbang "$0" "$@" ; exit $?
//DEPS info.picocli:picocli:4.5.0
import java.util.*;
import java.util.concurrent.Callable;
import picocli.CommandLine;
import picocli.CommandLine.Command;
import picocli.CommandLine.Parameters;
import static java.lang.System.*;
//import LL;
/**
* Detect if there is a cycle in a linked list
*
* https://www.youtube.com/watch?v=MFOAbpfrJ8g
* "a b,b c,c d,d b" to create a link list that cycles from d back to b.
*/
@Command(
name = "CycleInLinkedList",
mixinStandardHelpOptions = true,
version = "CycleInLinkedList 0.1",
description = "Detect if there is a cycle in a linked list")
class CycleInLinkedList implements Callable<Integer> {
@Parameters(index = "0", arity = "0..*", description = "Initial set of values to load into the linked list")
private List<String> inputStrings;
public static void main(String... args) {
int exitCode = new CommandLine(new CycleInLinkedList()).execute(args);
exit(exitCode);
}
@Override
public Integer call() throws Exception {
if (inputStrings == null) {
inputStrings = List.of(
// CYCLED
"a b,b c,c a",
"a b,b c,c b",
"a b,b c,c c",
// NOT CYCLED
"a b",
"a b,b c",
"a b,b c,c d");
}
inputStrings.forEach( str -> {
LL<Character> myLinkedList = new LL<>(str);
out.println(str + " : " + myLinkedList.hasCycle());
});
return 0;
}
}