Determine whether the linked list has a ring and find the ring entry

Source: Internet
Author: User

There is a single-chain table, where there may be a ring, that is, the next of a node points to the previous node in the linked list, so that a ring is formed at the end of the linked list.

Problem:

1. How to determine whether a linked list is such a linked list?
If the linked list contains an existing ring, what is 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 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 );
}

 

2. Find the entry point of the ring

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 entrance point of the ring. from the head of the linked list to the entry point of the ring, it can be seen that the entry point of the ring is equal to (n-1) the inner ring of the loop + the inner point of the ring, 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:

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

If the two linked lists overlap and the two linked lists are the same nodes from the intersection to the end of the linked list, we can traverse a 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.