Linked List Cycle

Easy

Topics
Linked ListTwo PointersHash Table

Given the head of a linked list, return true if it has a cycle. On this judge the input is the node values plus pos — the index the tail’s next connects to, or -1 for no cycle. pos is not passed to your function.

Example 1

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

Example 2

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

Constraints

  • 0 <= number of nodes <= 10^4
  • -10^5 <= Node.val <= 10^5
  • pos is -1 or a valid index.
Run ⌘' · Submit ⌘⏎