Thinking: there may be several cases.
- Acyclic list
- Completely cyclic list (all nodes are in the cycle/loop)
- Partially cyclic list (at least one node not in the cycle/loop)
Requirement for Case 1: O(n) in time and O(1) in space.
Idea: use 2 pointers for exchange and a 3rd one to temporarily record the next node.
Code:
Related Posts:
Discussion about Case 2
Discussion about Case 3
No comments:
Post a Comment