Linked List Cycle

Given `head`, 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`. Example: Input: head = [3,2,0,-4], pos = 1 Output: true Explanation: There is a cycle where tail connects to index 1. Pattern focus: Fast and Slow Pointers.

Input Format

head = ListNode

Output Format

return boolean

Constraints

  • The number of nodes is in the range [0, 10^4].
  • -10^5 <= Node.val <= 10^5
  • `pos` is the index of the cycle start or -1 if no cycle.

Examples

Example 1:

Input:

head = [1,2]

Output:

false

Example 2:

Input:

head = [1]

Output:

false

Example 3:

Input:

head = [3,2,0]

Output:

false
Loading...
Linked List Cycle - Linked List DSA Problem