Zero To DSAZero To DSA
Privacy Policy
Merge Two Sorted ListsRemove Nth Node From End

Linked List Cycle

easy
Time: O(n)
Space: O(1)

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

Constraints

  • The number of nodes is in the range [0, 10⁴].

Examples

Input: head = [3,2,0,-4], pos = 1
Output: true
Tail connects to node at index 1.
Input: head = [1,2], pos = 0
Output: true
Input: head = [1], pos = -1
Output: false