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.
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.
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.
Sign in to submit, save progress and mark problems as solved. You can still run your code.
Loading editor...
head =
[3,2,0,-4]
pos =
1
Expected
true