[Link to this article]
Http://www.cnblogs.com/hellogiser/p/linked-list-loop-and-intersections.html
(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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
|
/* Version: 1.0 Author: hellogiser Blog: http://www.cnblogs.com/hellogiser Date: */ 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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
|
/* Version: 1.0 Author: hellogiser Blog: http://www.cnblogs.com/hellogiser Date: */ 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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
|
/* Version: 1.0 Author: hellogiser Blog: http://www.cnblogs.com/hellogiser Date: */ 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.
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. For details, see 35. the first public node of the two linked lists [Find the first common node of two linked list].
[Reference]
Http://www.cnblogs.com/hellogiser/p/find-the-first-common-node-of-two-linked-list.html
Http://www.cppblog.com/humanchao/archive/2012/11/12/47357.html
Http://blog.csdn.net/liuxialong/article/details/6555850
[Link to this article]
Http://www.cnblogs.com/hellogiser/p/linked-list-loop-and-intersections.html