Files

25 lines
1.0 KiB
Plaintext

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 -> ...