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
[1,2,3,4,5], -10Example 2
[1], -10Example 3
[1,2], 01Example 4
[3,2,0,-4], 11[1,2,3,4,5], -1
0