2.6Given a circular linked list, implement an algorithm which returns the node at the beginning of the loop.
The quick pointer and the slow pointer begin to move in the head pointer, the fast pointer moves two steps at a time, the slow pointer moves one step at a time, until the meeting, after the meeting, the slow pointer points to the head pointer, and two pointers move backward one step. Until the meeting, where the meeting is the beginning of the cycle (if meet, indicating that there is a loop node, then the fast pointer to walk is twice times the slow pointer, fast: The start of the k distance, and a circle (or n-loop) cycle, and the slow pointer traversed the loop part of the length of the circuit portion of the three parts added ...
Look at the picture:
The distance of the slow pointer is the length of the part of the loop that begins without entering the loop, and the length of the circle that the slow pointer passes through is the two parts, and then the fast pointer is: the distance of K + slow in the loop and the length of the *n circle; The slow pointer goes one step at a time, so the fast length is equal to twice times the length of the Slow: get: (k + slow in the loop of the distance + the length of the full lap *n circle) =2* (k + slow in the loop distance) so:
The upper formula can become: K + slow in the circle of distance + full circle length *n ring =k+ Slow in the circle of the distance +k+ slow in the circle of the distance.
So you can get: the length of the full circle *n Circle =k+ slowly in the circle of distance;
If n is 1, then the length of the k is equal to the place where the current meeting begins. It is the place where the cycle begins and where the cycle begins.
If n is 2, then the K-to-cycle begins where the loop loops and encounters the place to the beginning of the cycle.
So, K to the place where the loop begins and where it meets to the beginning of the cycle.
So, a pointer in place, a pointer in the beginning, all go forward, the point where the meeting is the beginning of the cycle.
1 Public classCIRC6 {2 Static classLinknode {3 intVal;4 Linknode Next;5 6Linknode (intx) {7val =x;8Next =NULL;9 }Ten } One A Public StaticLinknode findbeginning (Linknode head) { -Linknode SLOWP =head; -Linknode FASTP =head; the - while(FASTP! =NULL&& Fastp.next! =NULL) { -FASTP =Fastp.next.next; -SLOWP =Slowp.next; + if(FASTP = =slowp) { - Break; + } A } at if(FASTP = =NULL|| Fastp.next = =NULL) { - return NULL; - } -SLOWP =head; - while(SLOWP! =FASTP) { -SLOWP =Slowp.next; inFASTP =Fastp.next; - } to returnslowp; + } -}
Another way to solve the problem is to see the Internet:
is to use HashSet, save each node, if the set exists in this node, it returns this node.
PublicLinknode FirstNodeInCircle1 (Linknode head) {if(Head = =NULL|| Head.next = =NULL) return NULL; Set<LinkNode> HashSet =NewHashset<linknode>(); while(Head! =NULL) { if(Hashset.contains (head)) {returnHead; } Else{hashset.add (head); Head=Head.next; } } return NULL; }
Reference:
Http://www.hawstein.com/posts/2.5.html
http://www.jyuan92.com/blog/careercup2_6-first-node-in-circle-linkedlist/
Https://github.com/1094401996/CareerCup/blob/master/Chapter02/src/twodot6/Circular.java
cc150 Chapter 2 | Linked Lists 2.6 Given A circular Linked list, implement an algorithm which returns node at the beginning of the loop.