Determine whether a single-chain table has a ring and find the ring entry point
AlgorithmDescription:
1. first determine whether a ring exists
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 non-ring linked list)
1 Bool Isexitsloop (list * Head) 2 { 3 List * slow = head, * fast = Head; 4 5 While (Fast & fast->Next) 6 { 7 Slow = slow-> Next; 8 Fast = fast-> next-> Next; 9 If (Slow = fast) Break ; 10 } 11 12 Return ! (Fast = NULL | fast-> next = Null ); 13 }
This problem can be extended to: Find the "Opposite" (far-remote) node of any node in the circular linked list. The algorithm is the same as above. When fast arrives at head (where head is any node) or head-> next, slow indicates the farthest node. The specific code is omitted.
2. After confirming that there is a ring in step 2, find the ring entry point:
Algorithm Description:
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 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, 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.
List * findloopnode (list * Head) {list * Slow = head, * fast =Head; While (Fast & fast-> Next) {slow = Slow-> Next; fast = Fast-> next-> 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 ;}
This problem can be extended to: determine whether two single-chain tables are intersecting. If yes, the first vertex of the intersection is given (both linked lists do not have loops ).
According to the problem description, two single-chain tables are merged into a single-chain table starting from the intersection, which is the key to understanding the algorithm.
Algorithm Description:
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. The specific code is omitted.