15 · Linked Lists

#141 Linked List Cycle

Easysolve on LeetCode ↗

frequently reported atBloombergMicrosoftAmazonGoogleMeta

the problem

In plain words

Given head, return true if some node can be reached again by following next (the list has a cycle), otherwise false.

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

out true

The tail links back to the node at index 1.

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

out true

The tail links back to the head.

in head = [1], pos = -1

out false

  • 0 ≤ number of nodes ≤ 10⁴
  • -10⁵ ≤ Node.val ≤ 10⁵
  • pos is -1 or a valid index (it is not passed to your function)

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. #142
      Linked List Cycle II
      Mediumin Linked Lists
    2. #287
      Find the Duplicate Number
      Mediumin Linked Lists
    3. #202
      Happy Number
      Easyin Math & Numbers