Determine if there are loops and lookup loops in the unidirectional list

Source: Internet
Author: User

Fast and Slow pointer algorithm description

Defines two pointers slow, fast. The slow pointer walks 1 nodes at a time, and the fast pointer walks 2 nodes at a time. If there is a ring in the list, then the slow pointer is bound to catch up with the fast pointer at some point (slow = = fast). If there is no ring, the fast pointer will first go to null.

Realize

The nodes are defined as follows:

class Node {    public Node next;    public Object data;    publicstaticint0;}

Algorithm:

/** * Speed pointer * @param head * @return  */     Public Static Boolean checkcircle(Node head) {Node fast =NULL; Node slow =NULL;        Fast = head; slow = head; while(true) {//Slow pointer move one step            if(NULL! = Slow.next) {slow = Slow.next; }Else{return false; }//Fast pointer move two steps            if(NULL! = Fast.next &&NULL! = Fast.next.next) {fast = Fast.next.next; }Else{return false; }//Check if you meet            if(slow = = fast) {return true; }        }    }
Algorithm description of Step count check

The above algorithm can only judge if there is no ring in the list, what if we want to find out the entrance of the ring?

Define two pointers p, Q. p each node (i.e. one step), Q goes backwards from the beginning until Q goes to null or P, Q goes to the same node but passes through a different number of steps. The number of steps at this point is the position of the ring entry in the node. If you go to null, there is no ring in the list.

Why does P, Q go to the same node but the number of steps is not equal when it indicates a ring? Because if p, q steps the same, indicating that they pass the node is the same, if p, q steps are different, then the P is from the ring and back to the ring of the entrance, when Q arrived at the node has not traversed the ring, so the number of steps is not equal, and the number of steps Q is the entrance of the ring.

Realize
/** * Find the beginning of the ring * @param head * @return Returns the index of the element, starting at 0. Return not Found-1 */     Public Static int Findcircleentry(Node head) {Node p = head;//Always start from scratchNode q = head;intPSteps =0;intQsteps =0; while(NULL! = q.next) {q = Q.next; ++qsteps;//P go from the beginning             while(NULL! = p.next) {p = p.next; ++psteps;//when P and Q point to the same node                if(p = = q) {//If the number of steps to walk is different, then this is the entrance                    if(PSteps! = qsteps) {returnPSteps-1; }Else{//Walk the same number of steps, not the entrance                         Break; }}} p = head;//Return to the head node.PSteps =0; }//One of the pointers went up to the head, stating no ring        return-1; }

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Determine if there are loops and lookup loops in the unidirectional list

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.