Detect Cycle In Linked List - aloalgo

Detect Cycle In Linked List

Easy

You are given the head of a singly linked list.

Your task is to determine if there is a cycle in this linked list.

Note that:

  • A cycle in a linked list means that some node in the list can be reached again by continuously following the next pointers.

Return true if a cycle is present, otherwise false.

Example 1

Input
1 → 0 → 1 → null
Output
False
Explanation:

The linked list is 1 -> 0 -> 1 -> null. There is no cycle.

Example 2

Input
1 → 2 → 3
    ↑   ↓
     ← ← 
Output
True
Explanation:

The linked list is 1 -> 2 -> 3 -> (back to node at index 1). There is a cycle.

Example 3

Input
None
Output
False
Explanation:

The linked list empty. There is no cycle.

Loading...
Input
1 → 0 → 1 → null
Output
False

Hello! I am your ✨ AI assistant. I can provide you hints, explanations, give feedback on your code, and more. Just ask me anything related to the problem you're working on!