EvalFrame Technical Mock Interviews & Coding Practice

Linked List Cycle

Easy · Linked List · Algorithm problem · all problems

Given `head`, the head of a linked list, determine if the linked list has a cycle in it. There is a cycle in a linked list if there is some node in the list that can be reached again by continuously following the `next` pointer. Internally, `pos` is used to denote the index of the node that tail's `next` pointer is connected to (0-indexed). It is `-1` if there is no cycle. Note that `pos` is not passed as a parameter. Return `true` if there is a cycle in the linked list. Otherwise, return `false`. **Examples:** ```typescript hasCycle([3,2,0,-4], 1) // true (tail connects to index 1) hasCycle([1,2], 0) // true hasCycle([1], -1) // false ```

Solve It in the BrowserPractice Explaining It Out Loud

Optimal Approach

  • Time complexity: O(n)
  • Space complexity: O(1)

Floyd's Cycle Finding algorithm runs in linear time. The fast pointer catches up to the slow pointer within at most the loop length.

Follow-Up Questions Interviewers Ask About Linked List Cycle

  • How does Floyd's cycle detection algorithm guarantee meeting?
    Because the fast pointer moves two steps while the slow moves one, the relative distance between them decreases by 1 in every iteration until they collide.
  • Can this be solved using extra memory?
    Yes, by tracking visited nodes using a Set or Hash Table with O(n) space complexity.

Practice the full problem with execution, tests, and AI code review in the interactive lab — then rehearse explaining your solution in a live voice mock interview.