Analyzing the circular linked list entry

Analyzing the circular linked list entry

1. Title Description

Given a list, return the first node into the beginning of the chain ring. If chain acyclic, null is returned. To show the list given ring, we integer pos connected to the end of the list to represent the position of the list (index starts from 0). If pos is -1, the ring is not on this list. Description: Do not allow to modify the given list.

Example 1:
Here Insert Picture Description
Example 2:
Here Insert Picture Description
Example 3:
Here Insert Picture Description

2. ideas (the double pointer Method)

Such list is generally used in the subject method to solve the double pointer, e.g. K seek distance of the tail node, loop to find the entrance to find a common inlet tail like.
Algorithmic process:
1. first encounter the double pointer: a pointer provided two FAST, slow the head of the list pointed head, fast walking round 2 steps, slow walk round Step 1;
(1) a first result: fast pointer through end of the list, the list described acyclic, direct return null; if ring, two pointers will meet. Because at every one pitch of the fast and slow + 1, fast will eventually catch up with slow;
(2) a second result: when == slow fast, two pointers first encounter in the ring. The following analysis of the relationship between the number of steps at this time through the fast and slow:
setting a total of a + b list nodes, wherein the head of the linked list to have a linked list entry, nodes (excluding ingress node list), the list b has a ring nodes (It should be Note that, a and b are unknown, for example, lists a = 4, b = 5 the diagram); provided two pointers are gone f, s steps, then:
1) the number of steps fast walking is twice slow the number of steps, i.e., = 2s f; (resolved: fast walk round 2 steps)
2) FAST than a slow walk the length of the loop of n, i.e., f = s + nb; (Resolution: double pointer through a step, and the ring until the windings overlap, to walk fast than when the overlap length of the loop is an integral multiple of slow);
3) Save the above two equations give: f = 2nb, s = nb , i.e., fast and slow down the pointers are 2n, n rings perimeter (Note: n is unknown, a different list of different situations).
2. Analysis of the current situation:
(1) If the pointer has been let go and count the number of steps k from the list head forward, then all walked a few steps when the node list entry is: k = a + nb (entry node to go a step, after each around an annular ring (b step) will again ingress node).
(2) At present, the number of steps walked pointer is slow nb step. Therefore, as long as we find ways to help slow walk a step to stop, you can go to the entrance of the ring.
(3) But we do not know the value of a, how to do? It is still using double pointer method. We construct a pointer, this pointer must have the following properties: a step to go forward with the pointer and SLOW this, both the ingress node coincide. Well, from where access nodes need to go a step? The answer is the head of the list head.
3. The second encounter double pointer:
(1) fast constant pointer position, the pointer will slow redirect head node list; fast and slow to move forward one step at the same time round; case f = 2nb, s = 0;
(2) when s = a slow walk step pointer, the pointer FAST come step f = 2nb + a, at this time two pointers coincide, while ring list entry point.
4. fast return node pointer.

3. Code

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
class Solution {
public:
    ListNode *detectCycle(ListNode *head) {
        ListNode* fast = head;
        ListNode* slow = head;

        while(true){
            if(fast == NULL || fast->next == NULL){
                return NULL;
            }
            fast = fast->next->next;
            slow = slow->next;
            if(fast == slow){
                break;
            }
        }
        slow = head;
        while(fast != slow){
            fast = fast->next;
            slow = slow->next;
        }
        return fast;
    }
};

4. Complexity Analysis

The time complexity of O (N): the second encounter, the number of walking slow pointer needs a <a + b; first encounter, the pointer must be slow walking number a + b - x <a + b, where x double pointer points to coincide with the inlet ring distance; so the overall complexity is linear;
space complexity O (1): two-hand use of additional spatial constant size.

Published 71 original articles · won praise 0 · Views 768

Guess you like

Origin blog.csdn.net/jiangdongxiaobawang/article/details/103987518