Details about intersection and expansion of two linked lists in the beauty of programming:
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?
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.
Set the length of H to C to A, the length of S to P to B, and the length of S to P to C. It is equivalent to two people starting from the H node and walking at the speed of 1 and 2 respectively. The encounter point is P, then, the first person takes the time h-> S-> P (A + B), and the second person takes 2 (A + B) in the time ), the route is H-> S-> P, that is, the length of a ring is longer than that of the first person, the distance is (A + B + C + B ). Then 2 (a + B) = (A + B + C + B) can obtain a = C, that is, PS = HS, that is, evidence.
4. in question 3, the length of the connection point distance header pointer has been obtained, and the length of the ring obtained in question 2 is the length of the single-chain table with a ring.
Programs that determine whether a ring exists:
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)
{
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 );
}
Find the program of the Ring Connection Point (Entry Point:
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;
}
The beauty of programming is an article about how to determine whether two linked lists meet each other. After reading the article, I think the original article is too cool. As a result, I have summarized that such problems can be extended into two categories:
1. http://blog.csdn.net/liuxialong/archive/2011/06/20/6555850.aspx of single-chain table and ring Problem
2. Single-chain table intersection and ring (this article)
--------------------------------------------------------------------------------
Given two single-chain tables A and B, only two pointers are given. Excuse me:
1. How do I determine whether two single-chain tables (without loops) are intersecting?
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.
2. 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)
3. How to find the first intersection node of a two-phase linked list (without knowing whether 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) Having loops: 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.