How to determine whether a single-link table has a ring

Source: Internet
Author: User

There is a single-chain table, where there may be a ring, that is, the next of a node points to the previous node in the linked list, so that a ring is formed at the end of the linked list.
The following two problems need to be solved: how to determine whether a linked list is such a linked list?
If the linked list contains an existing ring, how can we find the entry point of the ring?
To determine whether a linked list has a ring, 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, then fast must first enter the ring, and slow then enter the ring, the two pointers must meet. (Of course, if the first line to the end of fast is NULL, It is a loop-free linked list) The program is as follows:
01 bool I *** itsLoop (slist * head)
02 {03 slist * slow = head, * fast = head; 04 05 while (fast & fast-> next)
06 {07 slow = slow-> next; 08 fast = fast-> next; 09 if (slow = fast) break; 10} 11 12 return! (Fast = NULL | fast-> next = NULL); 13} When fast encounters slow, slow certainly does not traverse the linked list, and fast has 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:
1 2 s = s + nr 2 s = nr sets the length of the entire linked list L, the distance between the entrance ring and the encounter point is x, and the distance from the start point to the entrance point of the ring is.
1 a + x = nr 2 a + x = (n-1) r + r = (n-1) r + L-a 3 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. The program is described as follows:
01 slist * FindLoopPort (slist * head)
02 {03 slist * slow = head, * fast = head; 04 05 while (fast & fast-> next)
06 {07 slow = slow-> next; 08 fast = fast-> next; 09 if (slow = fast) break; 10} 11 12 if (fast = NULL | fast-> next = NULL)
13 return NULL; 14 15 slow = head; 16 while (slow! = Fast)
17 {18 slow = slow-> next; 19 fast = fast-> next; 20} 21 22 return slow; 23} determines whether two single-chain tables are intersecting. If yes, returns the first vertex of the intersection (neither of the two linked lists has a ring ). There are two better methods: connect one of the linked lists to the beginning and end, and check whether another linked list has a ring. If so, the two linked lists will intersection, the detected dependency ring entry is the first point of intersection.
If the two linked lists overlap and the two linked lists are the same nodes from the intersection to the end of the linked list, we can traverse a 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.

Related Article

Contact Us

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.