15 · Linked Lists

#142 Linked List Cycle II

Mediumsolve on LeetCode ↗

frequently reported atMicrosoftAmazonBloombergGoogle

the problem

In plain words

Return the node where the cycle begins, or null if there is no cycle. Do not modify the list.

in head = [3,2,0,-4], pos = 1

out node with value 2 (index 1)

in head = [1,2], pos = 0

out node with value 1 (index 0)

in head = [1], pos = -1

out null

  • 0 ≤ number of nodes ≤ 10⁴
  • -10⁵ ≤ Node.val ≤ 10⁵
  • Follow-up: O(1) memory

your turn

Try it before you peek

Read the examples, say the brute force out loud, then write code: here or on LeetCode ↗. Stuck for 20 minutes? Open one hint at a time.

Solve it in the browser

LeetCode-style: complete the class. Compiled by the real javac (Java 17) and judged right here — nothing is sent anywhere.

    Solutions are hidden so you can try first. Even 20 minutes of real effort makes the answer stick far better than reading it.

    Marking the problem solved, or opening every hint, also shows it.

    practise next

    Similar problems

    1. #141
      Linked List Cycle
      Easyin Linked Lists
    2. #287
      Find the Duplicate Number
      Mediumin Linked Lists