Skip to main content

Linked List Cycle

Given head, the head of a linked list, determine if the linked list has a cycle in it. There is a cycle in a linked list if there is some node in the list that can be reached again by continuously following the next pointer.

Problems23 / 152
Sign in

Linked List Cycle

EASYLinked ListTwo Pointers

Given head, the head of a linked list, determine if the linked list has a cycle in it. There is a cycle in a linked list if there is some node in the list that can be reached again by continuously following the next pointer.

Example 1

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

Example 2

Input:  head = [1,2], pos = 0
Output: true
Show hint

Use Floyd's cycle detection algorithm: two pointers moving at different speeds. If they meet, there's a cycle.

Draft saves automatically

Loading editor...

Submit also runs hidden tests
head =
[3,2,0,-4]
pos =
1
Expected
true
Run with your own input (JSON)