★linked List Cycle II--Leetcode

Source: Internet
Author: User

proving that the single-linked list has loops:

The algorithm used in this paper can be an image of the analogy is running in the playground. Fast speed will slow the buckle  


we can from P2 and P1 the location gap to prove. P2 will catch P1 but will not skip P1  

because P2 each walk 2 steps. And P1 took a step. So the gap between them is a step-by-step narrowing, 4,3,2,1,0 
< Span style= "font-family: Song body; Font-size:14px "> When it was 0, it was coincident.

find the starting point of the loop:

Since it is possible to infer whether there is a loop, how can we find the entrance to the loop?

Solutions such as the following: When P2 in accordance with each 2 steps, p1 each step of the way, found P2 and P1 coincident, determined the unidirectional chain list has
Loop out.



Next. Let P2 go back to the head of the list. Walk again, each step is not walk 2, but walk 1. So when P1 and P2 again
the time of meeting. Is the entrance to the loop.

This can be demonstrated by:

when P2 and P1 first met, assuming that the P1 walked the N-step, the loop's entrance was passed at P-step, then there was

1, p1 walk path: P+c = N, C is P1 and P2 intersection point, distance from the loop entrance.

2, p2 walk path: p+c+k*l = 2*n. L is the perimeter of the loop, K is an integer;

The 1-type p+c=n into 2-type, finishing: n=k*l;


So, assuming that starting from the P+c point, P1 will be able to return to the P+c point by taking the N step.

At the same time, p2 from the beginning. After n no, it will reach P+c.

Obviously in this step where P1 and P2 only have the previous P steps to go in different paths, so when P1 and P2 overlap again. Will
It is on the loop entry point of the linked list.


Code
Given a linked list, return the node where the cycle begins. If There is no cycle, return null. #include <iostream> #include <fstream>using namespace std;struct listnode {int val; ListNode *next; ListNode (int x): Val (x), Next (NULL) {}};class solution {Public:listnode *detectcycle (ListNode *head) {listnode* Fast_wal Ker = Head;if (Has_cycle (Head, Fast_walker)) {listnode* cur = head;while (fast_walker! = cur) {Fast_walker = Fast_walker-&gt ; next;cur = Cur->next;} return cur;} else return NULL;} Private:bool has_cycle (listnode* head, listnode* fast_walker) {listnode* Slow_walker = Head;while (Slow_walker & & Fast_walker) {Fast_walker = Fast_walker->next;if (fast_walker) Fast_walker = Fast_walker->next;else break; Slow_walker = slow_walker->next;if (Fast_walker = = Slow_walker) return true;} return false;}}; int main () {fstream fin ("test.txt"); Listnode* Head (0);//The storage address is not assigned at this time listnode* tmp = head;//is equivalent to TMP = Nullint in = 0;while (Fin >> in) {if (!head) {head = new ListNode (in); tmp = head;} Else{while (Tmp->next! = NULL) tmp = Tmp->next;tmp->next = new ListNode (in); tmp = Tmp->next;tmp->next = NUL L;}} Make a ring Tmp->next = head->next; Solution SS; listnode* Retult = ss.detectcycle (head); System ("pause"); return 0;}


★linked List Cycle II--Leetcode

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.