ExamAdda Logo

Linked List Cycle

Easy

Given the head of a linked list, determine if the linked list contains a cycle.

A cycle exists if there is some node in the list that can be reached again by continuously following the next pointer.

Return true if there is a cycle in the linked list. Otherwise, return false.

Example 1

Input

4
3 2 0 -4
1

Output

true

Explanation

The last node points back to the node at index 1, creating a cycle.

Example 2

Input

2
1 2
-1

Output

false

Explanation

The last node points to null, so the linked list does not contain a cycle.

Constraints

The number of nodes in the linked 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.

Hints:

Hint 1

Try using two pointers moving through the linked list at different speeds.

Hint 2

Use a slow pointer that moves one step at a time and a fast pointer that moves two steps at a time.

Author & Technical Reviewer

ExamAdda Technical Review Team

Technically reviewed by: ExamAdda Technical Review Team

Technical Reviewers, ExamAdda

Software engineers at ExamAdda who check every article's definitions, complexity claims and code examples before and after publishing.

Published

Aug 10, 2026

Last updated

Aug 10, 2026

Content Verification Methodology

Definitions and complexity claims were checked against authoritative computer-science references. Code examples were compiled and tested with standard, boundary and edge-case inputs.

Report a correction:editorial@examadda.org
Auto
Loading editor...
Input

Expected Output