Skip to main content
LeetCode 142, Medium. Topics: Hash Table, Linked List, Two Pointers. View on LeetCode. Generate this problem as a practice environment: tested reference solution, 15 parametrized pytest cases, and a playground notebook:

Problem

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

Examples

Example 1
Example 2
Example 3

Constraints

  • The number of the nodes in the list is in the range [0, 10^4].
  • -10^5 <= Node.val <= 10^5
  • pos is -1 or a valid index in the linked-list.
Follow up: Can you solve it using O(1) (i.e. constant) memory?

Solution

Reference implementation from solution.py on GitHub, full suite in test_solution.py:

Complexity

Tags

AlgoMaster 75.
Last modified on August 25, 2026