"Leetcode from zero single brush" Linked list Cycle

Source: Internet
Author: User

Topic:

Given A linked list, determine if it has a cycle in it.

Can you solve it without using extra space?

Answer:

At first glance it's simple. My practice is to save the head pointer, and then the other pointer continues through until the head pointer is encountered. But I did not take into account the partial loopback situation:

To tell the truth, I didn't think of any good way. Until the discussion, found that there can be a pursuit of the problem of the idea to do: a slow pointer to run a step at a time, a quick pointer run two steps at a time. If there is a ring must be able to catch up.

So I wrote down:

Class Solution {public:    bool Hascycle (ListNode *head) {        if (head = = NULL | | head->next = NULL | | head->next- >next = = NULL)            return false;                listnode* slow = head->next;        listnode* fast = head->next->next;        while (slow! = NULL)        {            if (slow = = fast)            {                return true;            }                        if (Fast->next->next = = NULL)            {                return false;            }            else            {                slow = slow->next;                Fast = fast->next->next;            }        }        return false;    }};

Show Runtime Error ... Later found, when judging if (Fast->next->next = = null), I forgot to consider: if Fast->next = = NULL, then Fast->next->next became a wild pointer /c23>, and then else, if you assign a field pointer to fast, there's no end to it.

The IF (Fast->next->next = = null) is determined to be changed to if (Fast->next->next = = NULL | | fast->next = = NULL), or wrong ...

Then the basic skills exposed ... The condition "or" operation is a short-circuit operation . and Fast->next = = NULL is a condition that needs to be judged in advance , so it should be judged beforehand. Final AC version:

Class Solution {public:    bool Hascycle (ListNode *head) {        if (head = = NULL | | head->next = NULL | | head->next- >next = = NULL)            return false;                listnode* slow = head->next;        listnode* fast = head->next->next;        while (slow! = NULL)        {            if (slow = = fast)            {                return true;            }                        if (Fast->next = = NULL | | fast->next->next = = NULL)            {                return false;            }            else            {                slow = slow->next;                Fast = fast->next->next;            }        }        return false;    }};

"Leetcode from zero single brush" Linked list Cycle

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.