Linked List Cycle Detection

Medium C Debugging Asked at Microsoft
Solve this problem

A corrupted free list has developed a loop and the allocator now hangs. Write a detector that reports whether a singly linked list contains a cycle, using constant extra memory.

Constraints

  • Signature: `int has_cycle(const Node *head)` returning 1 or 0.
  • O(1) additional memory - no visited set.
  • Must not modify the list.
  • Must handle head == NULL and a single self-referencing node.
  • No recursion.
Starter codeC
1typedef struct Node {2    int value;3    struct Node *next;4} Node;5 6int has_cycle(const Node *head) {7    // TODO8    return 0;9}