Search for a command to run...
You are given the head of a singly linked list.
Determine whether the linked list contains a cycle.
A cycle exists if a node can be revisited by continuously following the next pointers.
Return:
1 if a cycle exists
0 if no cycle exists
You must solve this problem using O(1) extra space.
[linked_list], pos
linked_list → list of node values
pos → index (0-based) where the last node connects
If pos = -1, there is no cycle
[3,2,0,-4], 1
1 or 0
1 ≤ N ≤ 100000
-10⁹ ≤ Node values ≤ 10⁹
pos ∈ [-1, N-1]
Expected Time Complexity: O(N)
Expected Space Complexity: O(1)
Example 1
[3,2,0,-4], 11Example 2
[1,2], 01Example 3
[1], -10Example 4
[1,2,3,4,5], -10[3,2,0,-4], 1
1