Detect Cycle in Linked List

Easy

Description

Given a linked list with potential cycle (last element points to index), return true if cycle exists.

Examples

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

Tail connects to node at index 1.

Input:head = [1,2], pos = 0
Output:true
Explanation:

Node 2's next pointer connects back to node 1 at index 0, forming a cycle.

Input:head = [1], pos = -1
Output:false
Explanation:

A single node with no cycle (pos = -1) has no next pointer looping back.

Constraints

  • 0 ≤ list length ≤ 10⁴

Ready to solve this problem?

Practice solo or challenge other developers in a real-time coding battle!