141. Linked List Cycle
Description
See: https://leetcode.com/problems/linked-list-cycle/
Solution
The strategy is to use two pointers, a walker and a runner (slow and fast). The walker (slow) moves forward one node at a time. The runner (fast) moves ahead 2 nodes at a time. So if there is a cycle, the runner will eventually lap the walker and the walker and runner will be at the same node. Otherwise the runner will reach the end of the linked list first.
Time Complexity: O(N).
Space complexity O(1)
Last updated