DescriptionQ405
Q405FWASIC interview problem
Detect a linked-list cycle
TechniquesLinked listFast and slow
DifficultyEasy
TopicLinked Lists
LanguageSystemVerilog
Requirements3 checkpoints
01
Problem
Return 1 when a singly linked list contains a cycle and 0 when it terminates at null.
Example input and output
Use this case to check your interpretationInput
1 -> 2 -> 3, with node 3.next pointing back to node 2Output
1 (cycle present)Explanation
The slow and fast pointers eventually meet inside the 2 -> 3 loop without modifying the list.
02
Requirements (3)
- Use constant extra space.
- Handle empty and one-node lists safely.
- Do not modify list links.
