You are given the head of a singly linked list that may contain a cycle. Your task is to return the value of the node where the cycle begins. If there is no cycle, return -1.
Example 1
Input: head = [3,2,0,-4], pos = 1
Output: 2
Explanation: The cycle starts at the node with value 2.
Example 2
Input: head = [1,2], pos = 0
Output: 1
Explanation: The cycle starts at the first node with value 1.
Example 3
Input: head = [1], pos = -1
Output: -1
Explanation: The linked list does not contain a cycle.
Approach, complexity and clean code in every language. Sign in to unlock the editorial.
Your submissions, results and runtime history live here. Sign in to start your streak.
1 2 3 4 5 1
2
1 2 0
1