Files

71 lines
4.6 KiB
Markdown

# linked-list
Companion code for the ankurm.com article **Linked List in Java: Singly, Doubly, Reverse, Cycle Detection and Interview Problems**
(a full rewrite of the 2015 posts "Implementing Singly Linked List in Java" and "Implementing Doubly Linked List in Java", now one article at the singly-list URL).
A singly and a doubly linked list built from scratch, with every operation's cost measured in links followed; then reverse, the middle node,
Floyd's cycle detection (and why its second phase works), merging sorted lists, remove-nth-from-end, palindrome, duplicates and intersection,
each cross-checked against an obviously-correct brute-force version on seeded random inputs. The two 2015 menu programs are kept and run
with the original keystrokes, which exposed a real bug in the 2015 doubly list. Every figure and console line in the article is quoted from a
file in `output/`, which the JUnit tests write while asserting the same numbers. There is no `docs/` folder: the explanations live in the article itself.
## Versions
| Component | Version | Verified against |
|---|---|---|
| JDK | Temurin 25.0.5.1+2 (LTS) | `java -version` |
| JUnit | 6.1.3 | latest GA on the junit-framework GitHub releases page; resolved from Maven Central by this build |
| Maven | 3.9.11 | `mvn -version` |
## Quickstart
```bash
cd linked-list
export JAVA_HOME=/path/to/jdk-25 PATH="$JAVA_HOME/bin:$PATH"
./scripts/run-all.sh # runs 15 tests and regenerates output/*.txt
java src/main/java/com/ankurm/interviewlab/linkedlist/LegacySinglyDemo.java # the 2015 singly list menu
java src/main/java/com/ankurm/interviewlab/linkedlist/LegacyDoublyDemo.java # the 2015 doubly list menu
```
The first Maven run must be online so the plugins land in the local cache.
## Source layout
```
linked-list/
├── pom.xml
├── scripts/run-all.sh
├── src/main/java/com/ankurm/interviewlab/linkedlist/
│ ├── ListNode.java value + next link; of(), withCycle(), show() that cannot hang on a cycle
│ ├── SinglyLinkedList.java addFirst/addLast/add/get/indexOf/remove*, with a hop counter and an optional tail pointer
│ ├── DoublyLinkedList.java next + prev links, O(1) addAfter/addBefore/remove(node), nodeAt from the nearer end
│ ├── LinkedListProblems.java reverse (2 ways), middle, hasCycle, cycleStart, cycleLength, merge, removeNth, palindrome, dedupe, intersection
│ ├── BruteForce.java slow, obviously-correct references (arrays, sorting, identity sets)
│ ├── DryRuns.java instrumented copies that record every step
│ ├── Pitfalls.java plausible-looking wrong versions
│ ├── LegacySinglyDemo.java the 2015 singly list menu program
│ └── LegacyDoublyDemo.java the 2015 doubly list menu program
├── src/test/java/.../ one test class per output file group
└── output/*.txt captured real output
```
## Captured output (`output/`)
| File | What it shows |
|---|---|
| `01-nodes-and-links.txt` | three nodes built by hand; inserting is one new object and one changed link |
| `02-singly-operations.txt` | every operation, the errors it throws, and the links each costs for n = 10 / 100 / 1000 |
| `03-delete-dry-run.txt` | the one pointer that changes when deleting from the middle, the head and the tail |
| `04-reverse-dry-run.txt` | three-pointer reversal pass by pass; recursion overflows the stack at 1,000,000 nodes |
| `05-middle-and-cycle.txt` | slow/fast pointers, a Floyd trace, and the meeting-point formulas checked on 930 shapes |
| `06-more-problems.txt` | merge trace, remove n-th from end, palindrome, duplicates, intersection |
| `07-doubly-list.txt` | both directions, O(1) unlink given a node, links followed from the nearer end |
| `08-legacy-bugs.txt` | the 2015 `insertAfter` never moved `last`: `deleteLast` removes the wrong node |
| `09-legacy-sentinel.txt` | the 2015 `removeFirst` returns 0 for both "removed a 0" and "list was empty" |
| `10-random-agreement.txt` | 400,000 seeded steps of both lists against `ArrayList`, 0 disagreements |
| `11-problems-agreement.txt` | 40,000 seeded cases of the interview problems against brute force, 0 disagreements |
| `12-pitfalls.txt` | five plausible mistakes, each run for real |
| `13-singly-session.txt` | the 2015 singly menu with the original keystrokes, identical to the 2015 sample output |
| `14-doubly-session.txt` | the 2015 doubly menu with the original keystrokes, identical to the 2015 sample output |
| `15-jdk-linkedlist.txt` | `java.util.LinkedList` is doubly linked, and `node(int)` starts from the nearer end (read from `lib/src.zip`) |