Linked list loop detection algorithm

两盒软妹~` 提交于 2019-12-02 20:01:46

Because the first (non-moving) pointer might not lie within the loop, so the pointers would never meet. (Remember that a loop may consist of only part of the list.)

Because maybe not the complete linkedList is within the loop.

For a linked list lasso detection algorithm, you need two pointers:

As long as the first pointer is where the cowboy is, no loop is detected. But if you move it stepwise forward, it will eventually enter the loop.


BTW, lasso is the terminus technicus from graph theory, cowboy isn't.

flight

Because the loop may not contain the element pointed to by the first pointer.

For example, if the first pointer points to element 1 and the linked list has a loop later on (1->2->3->4->2), your algorithm won't detect it.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!