Add linked-list module: singly and doubly lists with measured costs, reverse, middle, Floyd cycle detection, merge and 5 more interview problems, the 2015 menu programs and a doubly-list bug they exposed (15 tests, 15 transcripts)

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_017ic4b6r9iSyHu5qSJMzH2n
This commit is contained in:
Claude
2026-11-25 19:06:04 +05:30
parent 09f875ddd9
commit 515302d5ba
40 changed files with 3683 additions and 0 deletions
+13
View File
@@ -0,0 +1,13 @@
A linked list is nodes that point at each other
===============================================
a.val = 10 a.next.val = 20 a.next.next.val = 30 a.next.next.next = null
whole list from a: 10 -> 20 -> 30 -> null
length: 3
new node 15 points at 20: 15 -> 20 -> 30 -> null
a still reaches the old chain: 10 -> 20 -> 30 -> null
then a.next = the new node: 10 -> 15 -> 20 -> 30 -> null
one new object and one changed link; nothing else moved.
the empty list is null: ListNode.of() == null -> true
@@ -0,0 +1,21 @@
SinglyLinkedList: operations, errors and the links followed (hops)
==================================================================
addLast(10) -> [10]
addLast(30) -> [10, 30]
add(1, 20) -> [10, 20, 30]
addFirst(5) -> [5, 10, 20, 30]
remove(2) -> removed 20, list [5, 10, 30]
removeLast() -> removed 30, list [5, 10]
removeFirst() -> removed 5, list [10]
indexOf(10) -> 0 indexOf(99) -> -1
empty.removeFirst() throws NoSuchElementException: list is empty
list.get(5) throws IndexOutOfBoundsException: index 5, size 1
Links followed (hops) per operation, on a list that already holds n values:
n | addFirst | addLast no tail | addLast + tail | get(n/2) | indexOf(last) | removeFirst | removeLast
10 | 0 | 36 | 0 | 5 | 9 | 0 | 8
100 | 0 | 4851 | 0 | 50 | 99 | 0 | 98
1000 | 0 | 498501 | 0 | 500 | 999 | 0 | 998
("addLast no tail" and "addLast + tail" are the total hops to build the whole list of n by repeated addLast.)
+17
View File
@@ -0,0 +1,17 @@
Deleting from a singly linked list: which one pointer changes
=============================================================
remove the node at index 2 from 10 -> 20 -> 30 -> 40:
before: 10 -> 20 -> 30 -> 40 -> null
walk: before = node 20
found: before = 20, doomed = 30, doomed.next = 40
relink: 20.next = 40
after: 10 -> 20 -> 40 -> null
removing the head has no node before it, so the head variable itself changes:
before: head -> 10 -> 20 -> 30 -> null
head = head.next
after: head -> 20 -> 30 -> null
removing the tail needs the node before it, so it must be found by walking (and then tail moves back):
list 10, 20, 30, 40, 50: removeLast() returned 50 after following 3 links; list is now [10, 20, 30, 40]
+14
View File
@@ -0,0 +1,14 @@
Reversing 1 -> 2 -> 3 -> 4 with three pointers
==============================================
start prev=null cur=1 reversed: null still to do: 1 -> 2 -> 3 -> 4 -> null
pass 1 prev=1 cur=2 reversed: 1 -> null still to do: 2 -> 3 -> 4 -> null
pass 2 prev=2 cur=3 reversed: 2 -> 1 -> null still to do: 3 -> 4 -> null
pass 3 prev=3 cur=4 reversed: 3 -> 2 -> 1 -> null still to do: 4 -> null
pass 4 prev=4 cur=null reversed: 4 -> 3 -> 2 -> 1 -> null still to do: null
recursive version on the same list: 4 -> 3 -> 2 -> 1 -> null
iterative, 1,000,000 nodes: first value after reversing = 999999 (no problem)
recursive, 1,000 nodes on a 1 MB thread stack: ok
recursive, 1,000,000 nodes on a 1 MB thread stack: StackOverflowError
@@ -0,0 +1,40 @@
Middle node and cycle detection with a slow and a fast pointer
==============================================================
Middle node of 1 -> 2 -> ... -> n (for even n, the second of the two middles):
n = 1 -> middle value 1 (index 0)
n = 2 -> middle value 2 (index 1)
n = 3 -> middle value 2 (index 1)
n = 4 -> middle value 3 (index 2)
n = 5 -> middle value 3 (index 2)
n = 6 -> middle value 4 (index 3)
n = 7 -> middle value 4 (index 3)
n = 8 -> middle value 5 (index 4)
Floyd's algorithm on 3 -> 2 -> 0 -> -4 whose last node links back to index 1:
list: 3 -> 2 -> 0 -> -4 -> ... (last node links back to index 1)
step 0 slow at index 0 fast at index 0
step 1 slow at index 1 fast at index 2
step 2 slow at index 2 fast at index 1
step 3 slow at index 3 fast at index 3
they meet at index 3 after 3 steps: there is a cycle
phase 2: probe at index 0, slow at index 3
phase 2: probe at index 1, slow at index 1
they meet again at index 1 after 1 steps: that is where the cycle starts
cycleStart value = 2, cycleLength = 3, hasCycle = true
plain 3 -> 2 -> 0 -> -4: hasCycle = false, cycleStart = null, cycleLength = 0
Why the second phase works. Tail length mu (nodes before the cycle) and cycle length lambda:
mu lambda | steps until they meet | meet at index | phase-2 steps | cycle starts at index
0 4 | 4 | 0 | 0 | 0
1 10 | 10 | 10 | 1 | 1
3 4 | 4 | 4 | 3 | 3
4 4 | 4 | 4 | 4 | 4
5 4 | 8 | 8 | 5 | 5
7 3 | 9 | 9 | 7 | 7
30 30 | 30 | 30 | 30 | 30
checked all 930 combinations of mu = 0..30 and lambda = 1..30: the formulas held every time.
Memory: on a 100000-node ring, a set of seen nodes holds 100000 entries before it finds the repeat;
Floyd holds exactly two references (slow and fast) and reaches the same answer: hasCycle = true
+39
View File
@@ -0,0 +1,39 @@
Merge, remove n-th from end, palindrome, duplicates and intersection
====================================================================
Merge 1 -> 3 -> 5 and 2 -> 4 -> 6 -> 8 -> 9:
a: 1 -> 3 -> 5 -> null b: 2 -> 4 -> 6 -> 8 -> 9 -> null
compare 1 with 2 -> take 1 from a
merged so far: [1]
compare 3 with 2 -> take 2 from b
merged so far: [1, 2]
compare 3 with 4 -> take 3 from a
merged so far: [1, 2, 3]
compare 5 with 4 -> take 4 from b
merged so far: [1, 2, 3, 4]
compare 5 with 6 -> take 5 from a
merged so far: [1, 2, 3, 4, 5]
one list is empty; attach the rest of the other whole: 6 -> 8 -> 9 -> null
result: 1 -> 2 -> 3 -> 4 -> 5 -> 6 -> 8 -> 9 -> null
Remove the n-th node from the end of 1 -> 2 -> 3 -> 4 -> 5:
n = 1 -> 1 -> 2 -> 3 -> 4 -> null
n = 2 -> 1 -> 2 -> 3 -> 5 -> null
n = 3 -> 1 -> 2 -> 4 -> 5 -> null
n = 4 -> 1 -> 3 -> 4 -> 5 -> null
n = 5 -> 2 -> 3 -> 4 -> 5 -> null
n = 4 on a 3-node list throws IllegalArgumentException: n = 4 is longer than the list
Palindrome check (the list is put back the way it was):
1 -> 2 -> 3 -> 2 -> 1 -> null -> true list afterwards: 1 -> 2 -> 3 -> 2 -> 1 -> null
1 -> 2 -> 2 -> 1 -> null -> true list afterwards: 1 -> 2 -> 2 -> 1 -> null
1 -> 2 -> 3 -> 4 -> null -> false list afterwards: 1 -> 2 -> 3 -> 4 -> null
7 -> null -> true list afterwards: 7 -> null
null -> true list afterwards: null
Remove duplicates from sorted 1 -> 1 -> 2 -> 3 -> 3 -> 3 -> 4 -> 4 -> null
-> 1 -> 2 -> 3 -> 4 -> null
Intersection: a = 4 -> 1 -> 8 -> 4 -> 5 -> null b = 5 -> 6 -> 1 -> 8 -> 4 -> 5 -> null (the 8 -> 4 -> 5 tail is one shared chain)
the lists join at the node holding 8; both 1s before it are different nodes with the same value
a list that merely has the same values 8 -> 4 -> 5 but its own nodes: intersection = null
+28
View File
@@ -0,0 +1,28 @@
DoublyLinkedList: two links per node, and what that buys
========================================================
addLast(20), addFirst(40), addLast(60)
forward (next links): [40, 20, 60]
backward (prev links): [60, 20, 40]
addAfter(node 20, 45) and addBefore(node 20, 65)
forward: [40, 65, 20, 45, 60]
backward: [60, 45, 20, 65, 40]
addAfter(the last node, 99): the tail has to move, and it does
forward: [40, 65, 20, 45, 60, 99]
backward: [99, 60, 45, 20, 65, 40]
last().val = 99, removeLast() returned 99
Links followed to reach one element of a 1000-element list:
index | doubly (nearer end) | singly (from head)
0 | 0 | 0
250 | 250 | 250
499 | 499 | 499
500 | 499 | 500
750 | 249 | 750
999 | 0 | 999
Unlinking the element at index 700 when you already hold the node:
doubly: 0 links followed (the node knows its neighbours)
singly: 699 links followed (it must walk to the node before it)
+14
View File
@@ -0,0 +1,14 @@
Two defects in the 2015 programs, reproduced
============================================
2015 DoublyLinkedList: insertAtLast(1), insertAtLast(2), insertAtLast(3), then insertAfter(4, position 3)
Forward: 1 2 3 4
Reverse: 3 2 1
last.data = 3 (the new node 4 is the real last node, but 'last' was never moved)
deleteLast() returned 3; forward now: Forward: 1 2
it should have removed only 4 and returned 4; instead both 3 and 4 are gone and the caller saw only the 3.
the DoublyLinkedList in this module, same operations:
forward: [1, 2, 3, 4]
backward: [4, 3, 2, 1]
removeLast() returned 4
@@ -0,0 +1,8 @@
removeFirst() on an empty list, 2015 version and this module
============================================================
2015 list holding the value 0: removeFirst() returned 0
2015 list that is empty: removeFirst() returned 0 (and printed: List is empty - nothing to remove)
A caller that only looks at the return value cannot tell these two cases apart.
this module, list holding 0: removeFirst() returned 0
this module, now empty: removeFirst() throws NoSuchElementException: list is empty
@@ -0,0 +1,7 @@
Seeded random operations, compared with java.util.ArrayList after every step
============================================================================
seed 2026, 2,000 sequences of 100 operations per class, each step compared with the reference
SinglyLinkedList 200,000 steps compared, 0 disagreements
DoublyLinkedList 200,000 steps compared (forward and backward), 0 disagreements
@@ -0,0 +1,14 @@
Every problem against an obviously-correct brute-force version on seeded random lists
=====================================================================================
seed 2026, 5,000 random cases per problem
problem cases disagreements
reverse (iterative, recursive) 5,000 0
middle 5,000 0
mergeSorted 5,000 0
removeNthFromEnd 5,000 0
isPalindrome 5,000 0
removeDuplicatesSorted 5,000 0
hasCycle / cycleStart / cycleLength 5,000 0
intersection 5,000 0
+24
View File
@@ -0,0 +1,24 @@
Five plausible mistakes, each run for real
==========================================
1. Reverse that reads cur.next after overwriting it, on 1 -> 2 -> 3 -> 4:
wrong: 1 -> null
correct: 4 -> 3 -> 2 -> 1 -> null
2. Remove the value 1 from 1 -> 2 -> 3, looking only one node ahead:
wrong: 1 -> 2 -> 3 -> null (nothing was removed, and no error)
with a dummy node in front: 2 -> 3 -> null
3. Cycle check that tests fast but not fast.next, on the plain list 1 -> 2 -> 3:
wrong: throws NullPointerException
correct: hasCycle = false
4. Merge that forgets to attach what is left, 1 -> 3 -> 5 and 2 -> 4 -> 6 -> 7 -> 8:
wrong: 1 -> 2 -> 3 -> 4 -> 5 -> null
correct: 1 -> 2 -> 3 -> 4 -> 5 -> 6 -> 7 -> 8 -> null
5. Comparing nodes by value when the question is about identity (the intersection problem):
two separate lists 8 -> 4 -> 5: same value at the head? true same node? false
Printing a list that has a cycle never ends, so ListNode.show stops after 12 nodes:
1 -> 2 -> 3 -> 2 -> 3 -> 2 -> 3 -> 2 -> 3 -> 2 -> 3 -> 2 -> ...
+176
View File
@@ -0,0 +1,176 @@
The 2015 singly linked list menu program, same keystrokes as the original article
=================================================================================
keystrokes: 1 2 10 1 3 50 1 1 20 2 1 3 90 4 3 50 3 1 2 1 2 2 4 1 3 80 4 2 3 2 4 5
output identical to the 2015 article's sample output: true
--- Singly Linked List Menu ---
1. Insert
2. Remove
3. Search
4. Display
5. Exit
Enter choice: 1
1. Insert at position
2. Insert at first
3. Insert at last
Enter choice: 2
Enter element: 10
--- Singly Linked List Menu ---
1. Insert
2. Remove
3. Search
4. Display
5. Exit
Enter choice: 1
1. Insert at position
2. Insert at first
3. Insert at last
Enter choice: 3
Enter element: 50
--- Singly Linked List Menu ---
1. Insert
2. Remove
3. Search
4. Display
5. Exit
Enter choice: 1
1. Insert at position
2. Insert at first
3. Insert at last
Enter choice: 1
Enter element: 20
Enter position: 2
--- Singly Linked List Menu ---
1. Insert
2. Remove
3. Search
4. Display
5. Exit
Enter choice: 1
1. Insert at position
2. Insert at first
3. Insert at last
Enter choice: 3
Enter element: 90
--- Singly Linked List Menu ---
1. Insert
2. Remove
3. Search
4. Display
5. Exit
Enter choice: 4
List: 10 20 50 90
--- Singly Linked List Menu ---
1. Insert
2. Remove
3. Search
4. Display
5. Exit
Enter choice: 3
Enter element to search: 50
Element found at position: 3
--- Singly Linked List Menu ---
1. Insert
2. Remove
3. Search
4. Display
5. Exit
Enter choice: 3
Enter element to search: 1
Element not found
--- Singly Linked List Menu ---
1. Insert
2. Remove
3. Search
4. Display
5. Exit
Enter choice: 2
1. Remove first element
2. Remove last element
3. Remove element at position
Enter choice: 1
Removed: 10
--- Singly Linked List Menu ---
1. Insert
2. Remove
3. Search
4. Display
5. Exit
Enter choice: 2
1. Remove first element
2. Remove last element
3. Remove element at position
Enter choice: 2
Removed: 90
--- Singly Linked List Menu ---
1. Insert
2. Remove
3. Search
4. Display
5. Exit
Enter choice: 4
List: 20 50
--- Singly Linked List Menu ---
1. Insert
2. Remove
3. Search
4. Display
5. Exit
Enter choice: 1
1. Insert at position
2. Insert at first
3. Insert at last
Enter choice: 3
Enter element: 80
--- Singly Linked List Menu ---
1. Insert
2. Remove
3. Search
4. Display
5. Exit
Enter choice: 4
List: 20 50 80
--- Singly Linked List Menu ---
1. Insert
2. Remove
3. Search
4. Display
5. Exit
Enter choice: 2
1. Remove first element
2. Remove last element
3. Remove element at position
Enter choice: 3
Enter position: 2
Removed: 50
--- Singly Linked List Menu ---
1. Insert
2. Remove
3. Search
4. Display
5. Exit
Enter choice: 4
List: 20 80
--- Singly Linked List Menu ---
1. Insert
2. Remove
3. Search
4. Display
5. Exit
Enter choice: 5
Exiting...
+184
View File
@@ -0,0 +1,184 @@
The 2015 doubly linked list menu program, same keystrokes as the original article
=================================================================================
keystrokes: 1 1 20 1 1 40 1 2 60 1 3 45 2 1 4 65 2 3 1 3 2 2 1 2 2 3 1 4 20 2 3 2 3 1 5
output identical to the 2015 article's sample output: true
--- Doubly Linked List Menu ---
1. Insert
2. Remove
3. Display list
4. Search
5. Exit
Select option: 1
1. Insert at first
2. Insert at last
3. Insert after position
4. Insert before position
Select: 1
Enter element: 20
--- Doubly Linked List Menu ---
1. Insert
2. Remove
3. Display list
4. Search
5. Exit
Select option: 1
1. Insert at first
2. Insert at last
3. Insert after position
4. Insert before position
Select: 1
Enter element: 40
--- Doubly Linked List Menu ---
1. Insert
2. Remove
3. Display list
4. Search
5. Exit
Select option: 1
1. Insert at first
2. Insert at last
3. Insert after position
4. Insert before position
Select: 2
Enter element: 60
--- Doubly Linked List Menu ---
1. Insert
2. Remove
3. Display list
4. Search
5. Exit
Select option: 1
1. Insert at first
2. Insert at last
3. Insert after position
4. Insert before position
Select: 3
Enter element: 45
Enter position: 2
--- Doubly Linked List Menu ---
1. Insert
2. Remove
3. Display list
4. Search
5. Exit
Select option: 1
1. Insert at first
2. Insert at last
3. Insert after position
4. Insert before position
Select: 4
Enter element: 65
Enter position: 2
--- Doubly Linked List Menu ---
1. Insert
2. Remove
3. Display list
4. Search
5. Exit
Select option: 3
1. Forward display
2. Reverse display
Select: 1
Forward: 40 65 20 45 60
--- Doubly Linked List Menu ---
1. Insert
2. Remove
3. Display list
4. Search
5. Exit
Select option: 3
1. Forward display
2. Reverse display
Select: 2
Reverse: 60 45 20 65 40
--- Doubly Linked List Menu ---
1. Insert
2. Remove
3. Display list
4. Search
5. Exit
Select option: 2
1. Delete first
2. Delete last
3. Delete at position
Select: 1
Deleted: 40
--- Doubly Linked List Menu ---
1. Insert
2. Remove
3. Display list
4. Search
5. Exit
Select option: 2
1. Delete first
2. Delete last
3. Delete at position
Select: 2
Deleted: 60
--- Doubly Linked List Menu ---
1. Insert
2. Remove
3. Display list
4. Search
5. Exit
Select option: 3
1. Forward display
2. Reverse display
Select: 1
Forward: 65 20 45
--- Doubly Linked List Menu ---
1. Insert
2. Remove
3. Display list
4. Search
5. Exit
Select option: 4
Enter element to search: 20
Element found at position: 2
--- Doubly Linked List Menu ---
1. Insert
2. Remove
3. Display list
4. Search
5. Exit
Select option: 2
1. Delete first
2. Delete last
3. Delete at position
Select: 3
Enter position: 2
Deleted: 20
--- Doubly Linked List Menu ---
1. Insert
2. Remove
3. Display list
4. Search
5. Exit
Select option: 3
1. Forward display
2. Reverse display
Select: 1
Forward: 65 45
--- Doubly Linked List Menu ---
1. Insert
2. Remove
3. Display list
4. Search
5. Exit
Select option: 5
Exiting...
+24
View File
@@ -0,0 +1,24 @@
java.util.LinkedList, read from this JDK
========================================
JDK: 25.0.5.1+2-LTS
fields of java.util.LinkedList$Node (reflection): [Node next, Node prev, Object item]
interfaces of java.util.LinkedList: [List, Deque, Cloneable, Serializable]
java/util/LinkedList.java, node(int index), from line 578:
578 Node<E> node(int index) {
579 // assert isElementIndex(index);
580
581 if (index < (size >> 1)) {
582 Node<E> x = first;
583 for (int i = 0; i < index; i++)
584 x = x.next;
585 return x;
586 } else {
587 Node<E> x = last;
588 for (int i = size - 1; i > index; i--)
589 x = x.prev;
590 return x;
591 }
592 }