cheetcode

41. Linked List CycleEasy

Detect whether a linked list contains a cycle.

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

For the Cheetcode runner, represent the list as { values, pos } where pos is the index that the tail connects back to, or -1 for no cycle.

Work with node references instead of array indexes. Make sure you preserve the next pointers you still need before changing links.

Examples

Input: head = {"values":[3,2,0,-4],"pos":1}
Output: true
Input: head = {"values":[1,2],"pos":-1}
Output: false

Constraints

  • The number of nodes in the list is in the range [0, 10,000].
  • -100,000 <= Node.val <= 100,000
  • pos is -1 or a valid index in the list.