22/39
Part 4 of 7 · Cycles and meeting pointsLearn

Switching heads was the fastest of the three, with no memory

Measured on two lists of 400,000 and 300,000 nodes that share their last 100,000, median of 5 runs:

TimePeak memory
shared_by_set19.3 ms25,166,040 bytes
level the lengths21.9 ms356 bytes
shared_by_switching14.4 ms0 bytes

On two lists of 300,000 and 200,000 that never meet, the same three took 13.3 ms, 16.4 ms and 12.1 ms.

The two O(1) versions follow next the same number of times: counted on the first pair of lists, 1,200,000 each. Switching heads was faster because each step does less Python work, with no counter to add to and one loop instead of several.