365. Linked List Cycle II (Start of Cycle)

MediumLinked ListFloyd's Cycle DetectionTwo PointersLinked List

Given a linked list, return the index of the node where the cycle begins, or -1 if there is no cycle. The list is given as an array of values plus pos, the 0-based index that the tail's next pointer connects to (pos = -1 means no cycle). Return the cycle's start index. Input: '[list], pos'.

Input: '[list], pos'.

Output: Integer — cycle start index or -1.

Examples

Example 1
Input: [3,2,0,-4], 1
Output: 1
Explanation: Tail connects to index 1.
Example 2
Input: [1,2], 0
Output: 0
Explanation: Cycle starts at head.
Example 3
Input: [1], -1
Output: -1
Explanation: No cycle.

Constraints

Asked by

PaytmMicrosoftAmazonInfosysBloombergGoogle
Solve this problem in the editor →