+ 5
How do you check if a given linked list contains a cycle? How do you find the starting node of the cycle?
3 Antworten
+ 4
Use two pointers. Advance one pointer by one increment/node and the other by two. Repeat the process, checking at each iteration if the value of the two pointers are equal or if one has reached the end of the list. If the pointers are equal, then the linked list has a cycle and both pointers would point to the starting node of the cycle. If the end of the list/null node is reached, there is no cycle.
+ 2
Is that even allowed, such a cycle?
Can't you prevent that by testing if the same pointer already exists in that list, before you store one?
(I don't know about this well enough; I'll follow this and see what we will learn. 😉)
+ 2
Myself am looking into that. ..I saw the question elsewhere