MediumLinked ListTwo PointersHash Table

Linked List Cycle II

LeetCode
1 approach, code in all languages

Given the head of a singly linked list, return the node where a cycle begins. If the list has no cycle, return null.

A cycle occurs when the tail links back to an earlier node instead of terminating. That entry point is what you must locate. You should not modify the list, and ideally you solve it using only constant extra space.

Example 1

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

Output: Node at index 1 (value 2)

The tail loops back to the node with value 2, so the cycle begins there.

Example 2

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

Output: Node at index 0 (value 1)

The second node points back to the head, making the head the cycle entry.

Constraints

  • The number of nodes is in the range [0, 10^4].
  • -10^5 <= Node.val <= 10^5
  • pos is -1 (no cycle) or a valid 0-based index into the list.
You've got the patterns

Patterns get you through the screen. Shipping gets you hired.

FDE Coach is a cohort-based program in frontend, backend, AWS, and AI where you build real products and get referred to 200+ hiring partners. The free live workshop is the fastest way to see how we teach.

750+ engineers trained · frontend, backend, AWS & AI

August 15 · 0d left
Enroll Now