Bootcamps
Masterclasses
Menu
Courses
Account
Login / Sign Up

Detect Cycle In Linked List

Medium Acceptance 65% Points 30

You are given the head of a singly linked list. Your task is to determine whether the linked list contains a cycle. Return true if a cycle exists; otherwise, return false.

Examples
Example 1

Example 1

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

Output: true

Explanation: The last node points to the node at index 1, forming a cycle.

Example 2

Example 2

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

Output: true

Explanation: The last node points back to the first node, forming a cycle.

Example 3

Example 3

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

Output: false

Explanation: The linked list has no cycle.

Constraints
  • 1 = Number of nodes = 105
  • -10? = Node.data = 10?
Topics
Linked List
Companies
Microsoft
Added 25 Jul 2026 65% acceptance
Ln 1, Col 1Sign in to save your code
1 2 3 4 5
1
true