SDE Path

Detect a Cycle in a Linked List

Medium

Detect a Cycle in a Linked List

Determine whether a linked list contains a cycle using Floyd's algorithm.

Input format

The first line contains N and POS — the 0-based index that the last node links back to (-1 for no cycle). The second line contains the N node values. The judge builds the cycle for you.

Output format

Print true if the list contains a cycle, otherwise false.

Constraints

  • Values fit in a 64-bit signed integer
  • Trailing whitespace and a trailing newline are ignored by the judge

Read from stdin, write to stdout. Sample cases below show the exact format.

Sample cases

Example 1
Input
4 1
3 2 0 -4
Expected output
true
Example 2
Input
3 -1
1 2 3
Expected output
false