Problem, as an interviewer would state it: "Given the head of a linked list, determine whether it contains a cycle — using O(1) extra space, not a real Set tracking every visited node."
Examples:
hasCycle(acyclicListHead); // false
hasCycle(cyclicListHead); // true
Clarifying questions expected:
- Is O(1) extra space genuinely required, or would an O(n)-space visited-set approach be acceptable?
- Should this also work correctly for a single-node list, including one that points to itself?
- Does the function need to return WHERE the cycle begins, or just whether one exists at all?
Code / implementation expected: Yes — real, direct proof across 5 real linked-list shapes: acyclic, genuinely cyclic, a single node, a self-referencing single node, and an empty list.