Skip to interview prep
Apexed

93. How can you tell whether a linked list has even or odd length without counting every node one by one?

EasyLinked lists

Technical

Type or dictate your answer. Your draft is saved on this device.

Show model answer

Advance a pointer two nodes at a time. If it ends on null, the length is even; if it ends on a node whose next is null, the length is odd. This still visits about n/2 positions, so it is O(n) time and O(1) space, but it avoids keeping a counter.

This is mostly a warm-up question; the interviewer wants to see the two-step pointer pattern and correct null checks (`while (p && p.next) p = p.next.next`).

How can you tell whether a linked list has even or odd length without counting every node one by one — Data structures & algorithms interview question — Apexed