Problems

Linked List Cycle

easy
easy
linked-list
two-pointers
cycle-detection

An array nextIndices describes a linked list whose head is node 0; each value is the next node index or -1 for null. Return whether following next pointers enters a cycle.

Constraints

  • 0 <= nextIndices.length <= 10000
  • Every entry is -1 or a valid index

Follow-up

How does Floyd's slow/fast method translate to real node pointers?

Examples

Example 1

Input: nextIndices = [1,2,3,1]
Output: true

Example 2

Input: nextIndices = [1,-1]
Output: false

Example 3

Input: nextIndices = []
Output: false
🔒 5 hidden

Running will execute all 8 cases, including 5 hidden ones.