480 likes | 832 Views
Detecting a cycle L Detecting a cycle L p Detecting a cycle L p stack Detecting a cycle L p stack Detecting a cycle L p Detecting a cycle p Cycle detected at p L Now restore the list using the saved pointers p L Restoring . . . L p Restoring . . . L p Restoring . . .
E N D
Detecting a cycle L p stack
Detecting a cycle L p stack
Detecting a cycle p Cycle detected at p L
Restoring . . . L p
Restoring . . . L p
Restoring . . . L p
Restoring . . . L p
How much time and space? O(N)time p L O(N)space for stack
Detecting a cyclewith two-speed pointers L Cycle detected p q
Two-speed method:a larger example Cycle detected!
How much time and space? O(1)space O(N)time