Linked List Cycle Problem

Linked List Cycle Problem — ExecCode Easy DSA Practice

Solve the Linked List Cycle problem on ExecCode. Free online easy DSA practice in Arrays - Basics. Write and run code in Java, C++, Python — no signup required to run.

Problem description

Given head, the head of a linked list, determine if the linked list has a cycle in it. There is a cycle 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.

Examples

Input head = [3, 2, 0, -4], pos = 1; Output true. Input head = [1, 2], pos = 0; Output true. Input head = [1], pos = -1; Output false

Constraints

The number of nodes in the list is in the range [0, 10⁴]. -10⁵ ≤ Node.val ≤ 10⁵ pos is -1 or a valid index in the linked-list (pos is not passed as a parameter).

Practice Linked List Cycle free on ExecCode. Browse DSA problems, topic map, and placement guides.