Find the middle with slow and fast pointers
You do not need the length of a linked list to find its middle. Move one pointer one step and another two steps at a time. When the fast one reaches the end, the slow one is halfway.
# 1 -> 2 -> 3 -> 4 -> 5
# slow: 1, 2, 3 fast: 1, 3, 5Think before coding
The same two-speed idea detects cycles, because a fast pointer inside a loop eventually laps the slow one. For an even number of nodes, this version returns the second of the two middle nodes. Work through the example by hand before changing the starter. Decide what should happen for an empty input, a single value and a repeated value where those cases apply. The checks use several inputs so a hard-coded answer cannot stand in for the rule.
Your task
Node(value, next) is provided. Write middle(head) returning the middle node in one pass; for an even length return the second middle. Return None for an empty list.
The explanation opens here once every check passes.