Leetcode "Linked list": Linked list cycle && Linked list cycle II

Source: Internet
Author: User

1. Linked List Cycle

Topic links

Title Requirements:

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

Follow up:
Can you solve it without using extra space?

Just see this problem, it is easy to write down the following procedure:

1 BOOLHascycle (ListNode *head) {2ListNode *a = head, *b =head;3      while(a)4     {5b = a->Next;6          while(b)7         {8             if(A = =b)9                 return true;Tenb = b->Next; One         } AA = a->Next; -     } -      the     return false; -}

The biggest problem with this program is the "Dead Loop," which, for example, would go into a dead loop:

  

To solve this problem, we can define two pointers slow, fast,slow each step ahead, and fast each forward two steps. If the chain list exists in the loop, then after a certain number of cycles, slow and fast will certainly coincide (find an example deduction to understand). Specific from the program is as follows:

1 /**2 * Definition for singly-linked list.3 * struct ListNode {4 * int val;5 * ListNode *next;6 * ListNode (int x): Val (x), Next (NULL) {}7  * };8  */9 classSolution {Ten  Public: One     BOOLHascycle (ListNode *head) { A         if(head = = Nullptr | | head->next = =nullptr) -             return false; -          theListNode *slow = head, *fast =head; -          while(Fast! = nullptr && Fast->next! =nullptr) -         { -slow = slow->Next; +Fast = Fast->next->Next; -             if(Slow = =fast) +                 return true; A         } at          -         return false; -     } -};
2. Linked List Cycle II

Topic links

Title Requirements:

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?

The difficulty of the problem is still quite large. The specific solution is referenced from a blog post:

First look at the picture:

From the beginning of the list to the ring inlet length is: A, from the ring entrance to the faster and slower meeting point length is: x, the entire ring length is: C.

Assuming from the beginning to meet, slower traveled long for s, because the pace of faster is slower twice times, then faster in this period of time to walk a long distance of 2s.

And for faster, the distance he traveled is equal to the distance of the N-circle running around the entire ring, NC, plus the last time we met slower.

So we have:

2s = NC + S

For slower, the length of his walk is also equal to the distance from the beginning of the list to the point of encounter, so there are:

s = a + X

Through the above two-type descendant simplification has:

A + x = NC

A = Nc-x

A = (n-1) C + c-x

A = KC + (c-x)

Then you can see, C-x, is from the meeting point to continue to walk back to the ring entrance distance. Above the whole equation can be seen, if there is a pointer1 from the starting point and at the same time there is a pointer2 from the meeting point to continue to go forward (all only one step), then bypass the K Circle, Pointer2 will and Pointer1 in the ring entrance meet. In this way, a change of entrance is found.

The specific procedures are as follows:

1 /**2 * Definition for singly-linked list.3 * struct ListNode {4 * int val;5 * ListNode *next;6 * ListNode (int x): Val (x), Next (NULL) {}7  * };8  */9 classSolution {Ten  Public: OneListNode *detectcycle (ListNode *head) { Alistnode* slow =head; -listnode* fast =head; -          while(true){ the             if(!fast | |!fast->next) -                 returnnullptr; -              -slow = slow->Next; +Fast = Fast->next->Next; -             if(Slow = =fast) +                  Break; A         } at          -slow =head; -          while(Slow! =fast) { -slow = slow->Next; -Fast = Fast->Next; -         } in         returnslow; -     } to};

Leetcode "Linked list": Linked list cycle && Linked list cycle II

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.