Proof of single chain table ring problem __ Maths

Source: Internet
Author: User

Andrew Zhang
Mar 2, 2016

First, declare the definition of the node.

struct Node
{
    int val;
    Node* Next;
Public:
    Node (int v): Val (v), Next (NULL)
    {}
};

The symbols in this article are illustrated as follows:

1, to determine whether the linked list with the ring
To determine whether the linked list with a ring, you can set two pointers in the head node, one called fast, a call Slow,fast a walk two steps, and slow take a step. If there is a ring in the list, then fast and slow are bound to meet in the ring. If there is no ring in the list, then fast must first reach the tail node of the list before the slow pointer.
A mathematical model is established to prove the following:
The ring length is k and the link point number is 0,1,2,...,k-1. First of all, two nodes in the initial node 0, time t when the position of two pointers are t%k t\%k and 2t%k 2t\%k, when the node met
T%k=2t%k (1) t\%k=2t\%k \tag{1}
There must be a solution to this model, such as T equals K's integer multiple times this time just meets at the starting point. Now the main discussion will also meet at other points. Then the question is translated into Equation 2.
T%k=2t%k (2_1) t\%k=2t\%k \tag{2_1}
0<t%k<k (2_2) 0
Convert (2_1) to t%k= (t%k+t%k)%k t\%k= (t\%k+t\%k) \%k visible, the right side of the equation is not equal to the left side of the equation, only when both sides are zeros.
Now look at the case where the two pointers were not entered at the same time at the beginning, so the model is modified to
t%k= (2t%k+b)%k (3) t\%k= (2t\%k+b) \%k \tag{3}
Where the 0≤b<k 0\leq B indicates the position of the fast pointer where the slow pointer enters the loop, assuming that the entry node of the ring is numbered 0. The equivalent 3 solution is available (t+b)%k=0 (t+b) \%k=0. In summary, if there is a ring, two pointers must meet, and when the slow pointer arrives at the node k-b.
The code is as follows:

Class Solution
{public
:
    bool Hascirclainlist (Node * phead)
    {
        node* pfast = phead, *pslow = phead;< C5/>while (True)
        {
            if (pfast->next!= null && pfast->next->next!= null)
                pfast = pfast-> next->next;
            else return
                false;
            Pslow = pslow->next;
            if (pfast = = Pslow) return
                true;
        }
        return NULL;
    }
;

2, seek the ring long
When you know the intersection of two pointers within the loop, the number of steps to find a pointer inside the loop is the length of the ring.
Here is a question: Can you use the number of steps that the quick pointer walks before the first meeting minus the number of steps to go with the full pointer? The answer is no, because when the pointer enters the quick pointer may have been turned more than one lap, and if the first meeting point number is greater than K/2, the quick pointer after the full pointer into the loop in the second time to catch the full pointer to meet.

class Solution {public:int lengthofcircleinlist (node* phead) {node* pfast = PH
        EAD, *pslow = Phead;
        BOOL flag = FALSE;
                while (true) {if (Pfast->next!= null && pfast->next->next!= null)
            Pfast = pfast->next->next;
            else break;
            Pslow = pslow->next;
                if (pfast = = Pslow) {flag = true;
            Break
            } if (flag) {int length = 1;
            node* pnode = Pslow;
                while (Pslow->next!= pnode) {Pslow = pslow->next;
            length++;
        return length;
    else return 0; }
};

3. Find the entry point of the ring
Solve the idea, find the first encounter point, will pslow again point to Phead, while the pfast speed down to 1, wait until again meet the node is the entry node of the ring.
The mathematical proofs are as follows:
Suppose the meeting point node number is C, there are
T=a+c
2t=a+c+nk
So (a+c)%k=0, the proof.

Class Solution
{public
:
    node* entranceofcircleinlist (node* phead)
    {
        node* pfast = phead, *pslow = Phead;
        BOOL flag = FALSE;
        while (true)
        {
            if (pfast->next!= null && pfast->next->next!= null)
                pfast = pfast-> next->next;
            else break
                ;
            Pslow = pslow->next;
            if (pfast = = Pslow)
            {
                flag = true;
                break;
            }
        }
        if (flag)
        {
            pslow = Phead;
            while (Pslow!= pfast)
            {
                Pslow = pslow->next;
                Pfast = pfast->next;
            }
            return pslow;
        }
        else return
            NULL;
    }
;

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.