Live Batches
Masterclasses
Menu
Free Courses
Account
Login / Sign Up

Find Start of Cycle

Medium Acceptance 57.00% Points 30.00

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.

Examples
Example 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

Example 2

Input: head = [1,2], pos = 0

Output: 1

Explanation: The cycle starts at the first node with value 1.

Example 3

Example 3

Input: head = [1], pos = -1

Output: -1

Explanation: The linked list does not contain a cycle.

Hints
Hint 1
N/A
Constraints
  • 1 = Number of nodes = 105
  • -10? = Node.data = 10?
Companies
Amazon Microsoft Apple
Topics
Linked List
Solution.cs C#JavaPythonC++Javascript

Unlock the code editor

Sign in to write, run, and submit your solution against the full test suite.

  • Run code against sample & hidden test cases
  • Save submissions and track your streak
  • Compare with editorial & community solutions