29/39
Part 6 of 7 · The worked exampleLearn

Every exit goes through the restore

Test every short length, and check the list after each call.

for vals in ([], [4], [5, 5], [1, 2, 2, 1], [1, 2, 3, 2, 1], [1, 2], [1, 2, 3]):
    head = linked(vals)
    print(vals, reads_same_both_ways(head), values(head) == vals)
[] True True
[4] True True
[5, 5] True True
[1, 2, 2, 1] True True
[1, 2, 3, 2, 1] True True
[1, 2] False True
[1, 2, 3] False True

The last column checks that after every call the list holds its original values in order. That's a test you write for any function that changes its input and promises to put it back.

That's why the loop uses break and not return False. Returning from inside the loop would skip the restore. The caller's list would be left with its second half reversed and cut loose. Every exit goes through slow.next = reverse(second).