Pen highlights: Determine whether a single-chain table contains loops and Related Extension questions.

Source: Internet
Author: User

Originated from Network

1. How do I determine whether a single-chain table contains loops?

Set two pointers (fast and slow). The initial values all point to the header. slow moves one step forward each time, and fast moves two steps forward each time. If the linked list has a ring, fast must first enter the ring, when slow enters the ring, the two pointers must meet each other. (Of course, if the first line to the end of fast is NULL, It is a loop-free linked list) The program is as follows:

bool IsExitsLoop(slist *head){    slist *slow = head, *fast = head;    while ( fast && fast->next )     {        slow = slow->next;        fast = fast->next->next;        if ( slow == fast ) break;    }    return !(fast == NULL || fast->next == NULL);}

The following briefly explains why the two pointers finally met:

If the length of a single-chain table is n and the single-chain table is ring, p points to the I mod n element and q points to 2i mod n in the I iteration. Therefore, when I found 2i (mod n), p met q. While I (mod n) Then 2i (mod n) ----> (2i-I) mod n = 0 -----> I mod n = 0 ---> when I = n, p and q met. A simple understanding here is that p and q are running on the playground at the same time, where q is twice the speed of p. when both of them start at the same time, p is running at the starting point, q has just finished running two laps to reach the starting point.

What if p and q start point are different? Assume that p points to the element I mod n in the I iteration, and q points to k + 2i mod n, where 0 <k <n. So I (mod n) Then (2i + k) (mod n) ---> (I + k) mod n = 0 ----> when I = n-k, p and q met.

2. If the linked list shows an existing ring, how can we find its entry point?

When fast encounters slow, slow certainly does not traverse the linked list, and fast already loops n circles (1 <= n) in the ring ). Assume that slow takes the s step, then fast takes 2 s step (the number of fast steps is equal to the number of s plus n turns on the ring), set the ring length to r, then:

2 s = s + nr
S = nr

Set the length of the entire linked list to L. The distance between the entrance ring and the encounter point is x, and the distance from the start point to the entrance point is.
A + x = nr
A + x = (n-1) r + r = (n-1) r + L-
A = (n-1) r + (L-a-x)

(L-a-x) is the distance from the encounter point to the ring entry point. From this point, we can see that from the chain table header to the ring entry point = (n-1) loop inner ring + encounter point to the ring entry point, so we set a pointer from the head of the linked list and from the encounter point. Each time we take a step, the two pointers must meet each other and the first point of the encounter is the ring entry point. (In fact, you can also draw a graph to push the ring into a line segment. The column equation finally concluded that a + x = nx (n is an integer greater than 0 ))

The code is implemented as follows:

slist* FindLoopPort(slist *head){    slist *slow = head, *fast = head;    while ( fast && fast->next )     {        slow = slow->next;        fast = fast->next->next;        if ( slow == fast ) break;    }    if (fast == NULL || fast->next == NULL)        return NULL;    slow = head;    while (slow != fast)    {         slow = slow->next;         fast = fast->next;    }    return slow;}

Others

Expansion problems:

Determines whether two single-chain tables are intersecting. If yes, the first vertex of the intersection is given (both linked lists do not have loops ).

There are two better methods:

1. Connect one of the linked lists to the beginning and end, and check whether the other linked list has a ring. If so, the two linked lists are intersecting, and the detected dependency ring entry is the first vertex of the intersection.

2. If the two linked lists intersect, the two linked lists are the same nodes from the intersection to the end of the linked list, we can traverse one linked list first until the end, and then traverse another linked list, if you can also go to the same end point, the two linked lists will intersection.

Now let's write down the length of the two linked lists and traverse them again. The long chain table node starts to step forward (lengthMax-lengthMin), and then the two linked lists move forward simultaneously, each step, the first point of an encounter is the first point of the intersection of two linked lists.

 

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.