Thank you for your summary.
Http://blog.csdn.net/liuxialong/article/details/6555850
Http://blog.csdn.net/liuxialong/article/details/6556096
Question: Question 1, question 2, and question 3: How can we give a rigorous mathematical proof?
Given a single-chain table, only the early pointer H is given:
1. How to determine whether a ring exists?
2. How do I know the ring length?
3. How do I find out the connection points of the ring?
4. What is the length of the linked list with loops?
Given two single-chain tables A and B, only two pointers are given. Excuse me:
5. How can I determine whether two single-chain tables (without loops) are mutually exclusive?
6. How can I determine whether two single-chain tables (without knowing whether there is a ring) overlap?
7. How do I find the first intersection node of the two-phase linked list (I wonder if there is a ring?
Solution:
1. For question 1, use the catch-up method to set two pointers, slow and fast, starting from the beginning, each time one step and two steps forward. If a ring exists, the two will encounter each other. If no ring exists, fast will exit when null is encountered.
2. For question 2, record the collision point P, slow, and fast of Question 1 from this point, and the operand that the collision passes through is the length of the ring S.
3. Question 3: Theorem: the distance from the collision point P to the connection point = the distance from the header pointer to the connection point. Therefore, the distance starts from the collision point and the header pointer respectively, the point of the encounter is the connection point.
The proof of this theorem can be referred to: http://fayaa.com/tiku/view/7/
4. in question 3, we have obtained the length of the connection point distance header pointer, and the length of the ring obtained in question 2. The sum of the two is the length of the single-chain table with a ring.
Programs that determine whether a ring exists:
[CPP]
View plaincopyprint?
- 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 );
- }
Bool isexitsloop (slist * head) <br/>{< br/> slist * slow = head, * fast = head; </P> <p> while (Fast & fast-> next) <br/>{< br/> slow = slow-> next; <br/> fast = fast-> next; <br/> If (slow = fast) break; <br/>}</P> <p> return! (Fast = NULL | fast-> next = NULL); <br/>}
Find the program of the Ring Connection Point (Entry Point:
[CPP]
View plaincopyprint?
- 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;
- }
5. How can I determine whether two single-chain tables (without loops) are mutually exclusive?
There are two possible methods:
(1) artificially construct a ring. Point the tail node of linked list A to Linked List B, and then judge whether the ring is successfully constructed? Traverse from the head pointer of linked list B. If it can return to table B, it indicates the intersection.
(2) Determine whether the last node of the two linked lists is the same. If the two nodes are the same, the end node must be the same node.
6. How can I determine whether two single-chain tables (without knowing whether there is a ring) overlap?
First, judge whether there is a ring, and determine whether there is a ring. You can use the chase method to set two pointers, one for one step, and the other for two steps. If the two pointers match each other, the ring exists.
(1) There is no link between the two: Back to problem 1
(2) One with a ring and one without a ring: No need to judge. The two linked lists are definitely not intersecting.
(3) Both have loops: judge whether the collision point of linked list A appears in the ring of linked list B. (When the intersection is reached, the ring must be shared by two linked lists)
7. How do I find the first intersection node of the two-phase linked list (I wonder if there is a ring?
Similarly, use the chase method to determine whether a ring exists first and discuss the situation in detail.
(1) loop-free: A closed chain table is formed by pointing the End Node of chain table A to chain table B. This problem is converted to finding the ring entry node of a single-chain table with a ring.
Solution reference: http://blog.csdn.net/liuxialong/archive/2011/06/20/6555850.aspx
(2) loops:
This is my xjs.xjtu@gmail.com ideas:
Given that A and B are the same, the point of intersection between A and B can be considered as the intersection of A and B. In this way, you can separately find the connection points between the linked list and the ring for A or B.
The following is the original author's idea:
Calculate the length of the two linked lists (La and lb). (the sum of the length of the ring and the length from the ring to the entry point is the length of the linked list)
Calculate the length of the linked list with loops, refer to the http://blog.csdn.net/liuxialong/archive/2011/06/20/6555850.aspx
If La> LB, the linked list a pointer goes first la-LB, and then the Linked List B pointer starts to walk. The intersection of the two is the intersection.
If LB> La, the Linked List B pointer goes first LB-la, and then the linked list a pointer starts to walk. The intersection of the two is the intersection point.