Linked List Cycle Detection

Given the head of a linked list, determine if the linked list has a cycle in it. Return `true` if there is a cycle, otherwise return `false`.

Input Format

head = head node of the linked list

Output Format

true if a cycle exists, false otherwise

Constraints

Examples

Example 1:

Input:

head = [3,2,0,-4]

Output:

false

Explanation:

There is a cycle connecting tail to node index 1.

Example 2:

Input:

head = [1,2]

Output:

false

Explanation:

Cycle connecting tail to first node.

Example 3:

Input:

head = [1]

Output:

false

Explanation:

Single node without cycle.

Loading...
Linked List Cycle Detection - Two Pointers