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:
@@ -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 -> ...
|
||||
Reference in New Issue
Block a user