Neetcode lessons Flashcards

(1 cards)

1
Q

Find if there’s a loop in a linked list - how do I solve that? What method do I use?

A

Fast-runner slow runner:

If there’s a cycle, no matter where the fast runner is in the cycle, let’s say that when slow runner enters the cycle their distance is k, then on the next iteration their distance is going to be (k+1)%cycle_length etc, so they are bound to meet when k+i is divisible by cycle_length…

How well did you know this?
1
Not at all
2
3
4
5
Perfectly