The book is very complicated to write.
1. Detect if there is a loop on the link. Through the Fastrunner/slowrunner method, Fastrunner moves two steps at a time, Slowrunner one step at a time.
2, when the two met, F continue to walk, s go from the beginning. When they meet again, that's the entrance to the ring.
Suggestion: The fantasy is very difficult, suggest casually a few figures to see the law.
LinkedListNode findbeginning (LinkedListNode head) {LinkedListNode slow=Head; LinkedListNode Fast=Head; //Locate the collision place, and it will be in the loop_size-k of the linked list. while(Fast! =NULL&& Fast.next! =NULL) {Slow=Slow.next; Fast=Fast.next.next; if(Slow = =fast) { Break; } } //error checking, no collisions, no loops if(Fast = =NULL|| Fast.next = =NULL) { return NULL; } //slow start again, fast slows down and goes.slow =Head; while(Slow! =fast) {Slow=Slow.next; Fast=Fast.next; } returnFast;}
2.6 Given a linked list, an algorithm is implemented to return the opening node of the loop.