Limited time: AI code review, hints, mock interviews, whiteboard analysis, and all Pro features are unlocked. Enroll

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 ⌘⏎
SensAI SensAI
Beta
Listening...
Tap mic to stop voice mode

Unlock AI Features

One click to activate - no payment, no credit card. Just sign in and you're in.

AI code review and hints
SensAI chat assistant
AI mock interviews
Whiteboard analysis
100% free during early access