Finding loop in a singly linked-list

前端 未结 13 1791
梦谈多话
梦谈多话 2020-11-28 18:41

How can I detect that whether a singly linked-list has loop or not?? If it has loop then how to find the point of origination of the loop i.e. the node from which the loop

13条回答
  •  南笙
    南笙 (楼主)
    2020-11-28 19:15

    A quite different method:- Reverse the linked list. While reversing if you reach the head again then there is a loop in the list, if you get NULL then there is no loop. The total time complexity is O(n)

提交回复
热议问题