Written algorithm learning--linked list correlation

Source: Internet
Author: User

Given a single linked list, just give the pointer head: 1, how to determine if there is a ring.
2, how to know the length of the ring.
3, how to find out where the ring connection points.
4. What is the length of the linked list?


Solution:

1, for the problem 1, using a catch-up method, set two pointers slow, fast, starting from the beginning of the hands, each step forward 1 steps, 2 steps. If there is a ring, the two meet; fast encounters a null exit if no ring exists.

2, for the problem 2, recorded the problem 1 of the collision point P,slow, fast from the point of the beginning, each step forward 1 steps, 2 steps, the second collision is traversed by the number of operations is the length of the ring S.

3. Question 3: There is a theorem: the distance from the collision point P to the connection point = the distance from the head pointer to the connection point , so, starting from the collision point and the head pointer, the point of encounter is the connection point. (Proof in the back note)

4, the problem 3 has been found in the length of the connection point distance head pointer, plus the length of the ring found in Problem 2, the sum of which is the length of a single linked list with a ring


1. If there is a ring in the list of judgments can be set two pointers (Fast,slow), the initial values are pointed to the head, slow each step forward, fast each step forward two steps, if there is a ring in the list, fast first into the ring, and slow into the ring, two pointers in the ring must meet; If fast traverses to the tail is null, then no ring 2. Chain List has a ring, judge the entry point of the ring when fast and slow meet, slow certainly did not walk through the list, and fast has been circulating in the ring N-Circle (1<=n). Assuming that slow took the S-step, fast took 2s steps (the fast step is also equal to S plus the N-turns on the ring), the ring length is R, then:

2s = s + nr
s= NR

Set the entire chain table length L, the inlet ring and meet point distance is X, the distance from the starting point to the ring entry is a.
A + x = NR
A + x = (n–1) r +r = (n-1) R + l-a
A = (n-1) R + (l–a–x) (l–a–x) is the distance from the point of encounter to the ring entry point, so, from the chain head to the ring entry point equals (n-1) loop inner ring + meet point to the ring entry point (l-a-x for the distance from the meeting point to the ring entry point, how to understand, such as above, we assume SL ow and fast meet in point 3, starting at 1, the ring entry point is 2, the meeting point is 3, Gone (l-a-x) long distance after the return to 2 points. We set a pointer at the beginning and the ring meeting point each, each step, must meet. Meet must be at 2 o ' all, why. (according to the formula A: Head node to the distance of the entrance = (n-1) r: Ring length + (l–a–x): The distance between the meeting point to the entrance,) Thus, can be in the chain table head, meet the point to set a pointer, each time each step, two pointers must meet, then meet the 1th ring entry point .
Procedure to determine if a loop exists:

BOOL Isexitsloop (slist *head)    
{    
    slist *slow = head, *fast = head;    
    while (Fast && fast->next)     
    {    
        slow = slow->next;    
        Fast = fast->next->next;    
        if (slow = = fast) break;    
    }      
    Return! (fast = = NULL | | fast->next = = NULL);    
}   

A program that looks for a ring connection point (entry point):
slist* Findloopport (slist *head)    
{    
    slist *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;    
}   



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.