19. check whether a single-chain table has a ring? Find the entry point of the ring. Determine whether two single-chain tables are intersecting and find the intersection.

Source: Internet
Author: User

There is a single-chain table, which may have a ring, that is, the next of a node points to the node before it in the linked list,This forms a link at the end of the linked list.



Problem:

1. How to determine whether a linked list is such a linked list?
2. If the linked list contains an existing ring, how can we find the entry point of the ring?

Answer:

1. Determine whether the linked list has a ring. The method is as follows:

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 beginning and end of fast are null, it is a non-circular 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;

If (slow = fast) break;

}

Return! (Fast = NULL | fast-> next = NULL );

}


2. Find the entry point of the ring

If fast and slowWhen we met each other, slow certainly did not traverse the linked list, and fast had already loops n circles in the ring (1 <= N ). 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 is equal to (n-1) the cycle inner ring + the 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. The program is described as follows:

Slist * findloopport (slist * head)

{

Slist * slow = head, * fast = head;

While (Fast & fast-> next)

{

Slow = slow-> next;

Fast = fast-> 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;

}



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. That is to say, traverse the first linked list to find the last element, traverse the last element, and find the last element. If the two end elements are the same, they are the same.


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.