Linked List Cycle

Asked byAmazonSpotifyMicrosoftOracleVisaGoogle

Problem

Given the head of a linked list, determine whether the list contains a cycle — that is, whether some node can be reached again by continuously following the next pointer.

Examples

Example 1
Input:head = [3,2,0,-4], tail connects to index 1
Output:true
Example 2
Input:head = [1], no cycle
Output:false

Constraints

  • The number of nodes is in the range [0, 10^4].

Solve it in the editor. Sign in free to run your Python or JavaScript against test cases, get a verdict, and track your attempts.

Solve on FeatCode →

How to approach it: the Linked List pattern

A chain of nodes, each pointing to the next. No random access like an array, but insertion and deletion at a known position is O(1), and many classic problems come down to careful pointer rewiring.

Look for this pattern when

  • The problem explicitly gives you a linked list (reverse it, merge lists, detect a cycle, reorder it).
  • You need O(1) insertion/deletion without shifting elements, unlike an array.

Read the full Linked List guide →

Video walkthroughs

Original problem on LeetCode ↗

More Linked List problems