Count number of nodes in a linked list that may be circular

前端 未结 4 858
长情又很酷
长情又很酷 2021-01-05 17:08

Here is the problem, it is from Sedgwick\'s excellent Algorithms in Java (q 3.54)

Given a link to a node in a singly linked list that contains no null links (i.e. ea

4条回答
  •  情深已故
    2021-01-05 17:34

    The tortoise and hare algorithm can give you both the cycle length and the number of nodes before the cycle begins (λ and μ respectively).

提交回复
热议问题