Chaturmind
LearnDSASystem DesignBlogPremium
Sign inGet started
Chaturmind

Structured learning paths for engineers who want to go deep. Written by practitioners.

Learn

  • Java
  • DSA
  • System Design
  • Spring Boot
  • AI / ML

Company

  • Blog
  • Premium
  • Contact

Legal

  • Privacy Policy
  • Terms of Service

© 2026 Chaturmind. All rights reserved.

Built for engineers who want to go deep.

DSA›Linked Lists›Linked List Cycle
EasyLinked Lists

Linked List Cycle

linked-listfast-slow-pointersfloyd

Problem

Given head, the head of a linked list, determine if the linked list has a cycle in it.

Return true if there is a cycle, false otherwise.

Examples

Example 1

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

Output: true

Explanation: Tail connects to node at index 1.

Example 2

Input: head = [1], pos=-1

Output: false

Explanation: No cycle.

Constraints

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

Hints

Hint 1

Floyd's cycle detection: fast pointer moves 2 steps, slow moves 1 — they meet if there's a cycle.

Solutions

public boolean hasCycle(ListNode head) {
    ListNode slow = head;
    ListNode fast = head;
    while (fast != null && fast.next != null) {
        slow = slow.next;
        fast = fast.next.next;
        if (slow == fast) return true; // they met — cycle exists
    }
    return false;
}
Java

Time: O(n) · Space: O(1)