Linked List Interview Prep
Five harder prompts on cycle detection, in-place reversal of a sublist, and merging two sorted lists. Code-anchored with one bug hunt.
Question Bank
Hard
JavaScript
linked-list
cycle-detection
interview-prep
algorithms
585 views
14
Implement hasCycle(head) using Floyd's tortoise-and-hare. Return true if the list has a cycle, false otherwise. Aim for O(1) extra space.
Examples
Example 1:
Input: list 1 -> 2 -> 3 -> 4 -> 2 (cycle from 4 back to 2)
Output: true
Explanation: slow advances by 1 and fast by 2. Inside the cycle, fast gains one step on slow per iteration and must catch up within L steps.Example 2:
Input: list 1 -> 2 -> 3 -> null
Output: false
Explanation: fast (or fast.next) becomes null before the pointers meet, indicating no cycle.4 more questions, with full solutions and explanations, are available for premium members.
Upgrade to Premium