"Leetcode" "C + +" Linked List cycle 2

Source: Internet
Author: User

Given a linked list, return the node where the cycle begins. If There is no cycle, return? null .

Follow up:

Can you solve it without using extra space?

This problem is not difficult. But do not want to extra space to make or to move a bit of brains, because I have seen similar algorithm problems. So it was very quick to think of.

The method is to use two pointers from the beginning, the pointer P1 one step at a time, the pointer P2 two steps at a time, assuming that there is a ring two pointers must be reunited (this is used in the linked List cycle).

Then it is how to find the starting node of the ring.

It can be assumed that the distance from the beginning of the chain to the beginning of the ring is called a. The length of the ring is called C, and the starting distance of the first encounter position from the ring is p. First P1 by P2 when it must not go through the whole ring (think why?) That is to say 0<p<c. So from the departure to meet, P1 traversed the distance for a+p,p2 traversed by the distance of A+P+NC (n is a positive integer). And because P2 speed is p1 twice times, so there is

2A+2P=A+P+NC, so there are a+p=nc.

Now both pointers are positioned at the beginning of the ring over p. Then walk a a distance to return to the beginning of the ring.

And a happens to be the distance from the beginning of the chain to the beginning of the ring, so we have another pointer back to the beginning of the chain, there is a pointer still in place, at the same time with Speed 1 forward, meet again must be at the beginning of the ring.

/** * Definition for singly-linked list. * struct ListNode {*     int val; *     ListNode *next; *     listnode (int x): Val (x), Next (NULL) {} *}; */class Soluti On {public:    listnode *detectcycle (ListNode *head) {        ListNode *p1=head;        ListNode *p2=head;                BOOL hascycle = false;        if (!head) return NULL;        while (p2->next!=null) {            P1 = p1->next;            P2 = p2->next->next;            if (P2 = = null) return null;            if (P1 = = p2) {                hascircle = true;                break;            }        }        if (!hascycle) return NULL;        else{            p2 = head;            while (P1!=P2) {                P1 = p1->next;                P2 = p2->next;            }            return p1;}}            ;


"Leetcode" "C + +" Linked List cycle 2

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.