Diwali Offer!Flat 20% OFF
Enroll Now
PrakalpanaLive online tech training

71. Linked List Cycle II

MediumDay 21 of 60Linked List

Given the head of a linked list, return the node where a cycle begins, or null if there is no cycle. Do not modify the list. In the tests, pos is the index the tail links back to (-1 for no cycle) and is not passed to your function; the judge reports the index of the node you return, or -1 for null.

Example 1
Input: head = [3,2,0,-4], pos = 1
Output: 1
The cycle starts at the node with index 1 (value 2).
Example 2
Input: head = [1,2], pos = 0
Output: 0
Example 3
Input: head = [1], pos = -1
Output: -1
No cycle, so return null.
Constraints
  • The list has 0 to 10^4 nodes
  • -10^5 <= Node.val <= 10^5
  • `pos` is -1 or a valid index
Hints
Frequently asked at: Amazon, Microsoft, Google, Goldman Sachs
Ctrl + Enter runs the examples

Not sure what to learn next?

A 1-on-1 session with an industry mentor and a roadmap built for you. How it works

  1. 1Discuss your goals
  2. 2Get a personal plan
  3. 3Start learning
Book for β‚Ή200/ 30 min