cycle-detection

Why does Floyd's cycle finding algorithm fail for certain pointer increment speeds?

谁说我不能喝 提交于 2019-12-13 22:19:03
问题 Consider the following linked list: 1->2->3->4->5->6->7->8->9->4->...->9->4..... The above list has a loop as follows: [4->5->6->7->8->9->4] Drawing the linked list on a whiteboard, I tried manually solving it for different pointer steps, to see how the pointers move around - (slow_pointer_increment, fast_pointer_increment) So, the pointers for different cases are as follows: (1,2), (2,3), (1,3) The first two pairs of increments - (1,2) and (2,3) worked fine, but when I use the pair (1,3),