Determine whether a single-chain table has a ring. If yes, find the entry position of the ring => Find the intersection of the two linked lists.

Source: Internet
Author: User

Http://beyrens.blog.163.com/blog/static/9589445220081013332452/

First, how to determine whether a linked list has a ring:

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;
If (slow = fast) break;
}

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

The following shows how to find the entrance to this ring:

Method 1: When fast and slowEncounterSlow certainly does not traverse the Linked List (this is because if the linked list is a ring, slow will catch up with slow when it just reaches the end of the linked list, when this ring is not the whole linked list, it must be caught up by fast when slow reaches the end ), fast has 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:
Because 2 S = S + nR
So we can conclude that S = nR
Set the length of the entire linked list to L,Ring entrance and meeting pointThe distance is X, and the distance from the start point to the entry point of the ring is.
Then there is a + x =
S releases a = nR
Decomposed into a + x = (n-1) R + r = (n-1) R + L-
A = (n-1) R + (L-a-x)
(L-a-x) isEncounter point and ring entrance pointFromThe entry point from the linked list header to the ring is equal(N-1) Cyclic inner ring + encounter point and ring entry pointSo we set a pointer from the head of the linked list and the encounter point respectively. Each time we take a step, the two pointers must meet each other and the first point of the encounter is the ring entrance 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;
}

 

Another method is to use a pointer to store the address of the node to a vector every time you traverse the node. During the next traversal, you can check whether the node exists from the vector, if a node in the vector appears before the pointer is null, a ring exists.

 

The following describes how to find the intersection point from two linked lists. In fact, this is a derivative problem of the linked list ring.

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.