My Solution
Solution 1
The Key Idea for Solving This Coding Question
Use hash set to record the visited nodes. Once find a visited node in the hash set, there is a cycle in the linked list.
C++ Code
Time Complexity
is the number of nodes in the linked list referred by head
.
Space Complexity
Solution 2 (Floyd Cycle Detection Algorithm)
The Key Idea for Solving This Coding Question
C++ Code
Time Complexity
is the number of nodes in the linked list referred by head
.
Space Complexity
Solution 3
The Key Idea for Solving This Coding Question
C++ Code
Time Complexity
is the number of nodes in the linked list referred by head
.
Space Complexity
Solution 4 (Brent Cycle Detection Algorithm)
The Key Idea for Solving This Coding Question
C++ Code
Time Complexity
is the number of nodes in the linked list referred by head
.
Space Complexity
Reference
探索 Floyd Cycle Detection Algorithm
Miscellaneous