# 65. Analysis and Summary of problems related to whether the linked list has a ring, a ring entry, a ring length, and a linked list Intersection

Source: Internet
Author: User

(1) Does the linked list have 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 and set the collision point to p. (Of course, If fast is NULL, It is a loop-free linked list) The program is as follows:

[Code]

C ++ Code
 123456789101112131415161718192021 /*Version: 1.0Author: hellogiserBlog: http://www.cnblogs.com/hellogiserDate:*/Bool IsExitsLoop (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 false;Return true;}
(2) Find the entry point of the ring?

Theorem:Slow and fast start to p, so that slow starts from head, fast starts from p, and each time goes one step further until slow and fast meet again, the encounter point is the entrance of the ring.

Proof:

When fast encounters slow, slow certainly does not traverse the linked list, and fast already loops n circles (n> = 1) 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 ring entry and the encounter point is x, and the distance from the start point to the ring entry point is.

Then s = a + x, L = a + r. Then a + x = nr = (n-1) r + r = (n-1) r + L-a, then 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, therefore, we set a pointer from the linked list header and 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 entrance point.

[Code]

C ++ Code
 1234567891011121314151617181920212223242526272829 /*Version: 1.0Author: hellogiserBlog: http://www.cnblogs.com/hellogiserDate:*/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;}
(3) how to know the length of the ring?

Record the collision points meet. The slow and fast start from this point. The operands that the collision passes through again are the length of the ring r.

[Code]

C ++ Code
 123456789101112131415161718192021222324252627282930313233 /*Version: 1.0Author: hellogiserBlog: http://www.cnblogs.com/hellogiserDate:*/Unsigned int GetLoopLength (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 0;Slist * meet = slow;Slow = meet-> next;Fast = meet-> next;Unsigned int len = 1;While (slow! = Fast){Len ++;Slow = slow-> next;Fast = fast-> next;}Return len;}
(4) What is the length of the linked list with loops?

L = a + r.

(5) determine whether two single-chain tables are at the same intersection?

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 list L2 at the beginning and end to check whether another linked list L1 has a ring. If so, the two linked lists intersect, the detected dependency ring entry is the first point of intersection.

[Reference]

Http://www.cppblog.com/humanchao/archive/2012/11/12/47357.html

Http://blog.csdn.net/liuxialong/article/details/6555850

Related Keywords:
Related Article

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.