Problem:
Given A linked list, determine if it has a cycle in it.
Follow up:
Can you solve it without using extra space?
https://oj.leetcode.com/problems/linked-list-cycle/
Problem II:
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?
https://oj.leetcode.com/problems/linked-list-cycle-ii/
Analysis
at first, the Complexity O (n^2) method was used, using two pointers A, B. A step forward from the beginning of the table, encounter null is not a ring, return false;a each step, B go from the beginning, if you encounter B==a.next, then there is a ring true, if you encounter b==a, then there is no ring, continue to loop.
Later, we found the complexity O (n) method, using two pointers slow,fast. Two pointers starting from the table head, slow each step, fast each walk two steps, if fast encountered null, then no ring, return false, if slow==fast, the description has a ring, and at this time fast super slow a lap, return true.
Why do they meet when there are rings? Because Fast enters the ring first, after the slow enters, if the slow is regarded as in front, fast in the back each cycle all to slow near 1, therefore certainly will meet, but does not appear fast directly skips slow the situation.
Scaling issues
In the online collection of some of the problems related to the question, the idea of a lot of open, summed up as follows:
1. What is the length of the ring?
2. How do I find the first node in the ring (that is, linked list Cycle II)?
3. How to turn a linked list into a single linked list (release ring)?
4. How can I tell if there is an intersection of two single-linked lists? How do I find the first node that intersects?
First we look at the following picture:
Set: The chain header is X, the first node of the ring is y,slow and fast the intersection of the first is Z. The lengths of each segment are a,b,c, respectively. The length of the ring is L. The speed of slow and fast is QS,QF respectively.
Let's go through the problem analysis.
1. Method One (online is the answer):
After the first encounter, let Slow,fast continue to walk, recording to the next encounter cycle a few times. Because fast went a lap when fast came to the z point for the second time, and slow walked half a circle, and when Fast came to the z point for the third time, fast went two laps, and slow walked around, just as the Z-spot met.
Method Two:
After the first encounter, let fast stop not to go, slow continue to walk, recorded to the next encounter cycle a few times.
Method Three (simplest):
The first encounter when slow traversed distance: A+b,fast traversed distance: a+b+c+b.
Because Fast is twice times the speed of slow, fast is twice times the distance from slow, with 2 (a+b) = A+b+c+b, which can be a=c (this conclusion is important!). ).
We find l=b+c=a+b, that is to say, from the beginning to the first encounter, the number of cycles equals the length of the ring.
2. We have reached the conclusion a=c, then let two pointers starting from X and Z, each step, then just meet in Y! That is, the first node of the ring.
3. At the end of the previous question, disconnect the node that precedes the Y point in section C with the link to Y.
4. How can I tell if there is an intersection of two single-linked lists? First of all, to determine whether the two linked list has a ring, if a ring has no ring, certainly do not intersect, if two have no ring, determine whether the tail of two lists is equal; If two have rings, determine if the z-point on the list is on the other linked list.
The Linked List Cycle Code is as follows:
Class Solution {public: bool Hascycle (ListNode *head) { if (head==null) return false; if (Head->next==null) return false; ListNode *fast=head,*slow=head; while (Fast->next&&fast->next->next) { fast=fast->next->next; slow=slow->next; if (Fast==slow) { return true; } } return false; }};
The Linked List Cycle II code is as follows:
Class Solution {public: listnode *detectcycle (ListNode *head) { if (head==null) return NULL; if (head->next==null) return NULL; ListNode *fast=head,*slow=head; while (Fast->next&&fast->next->next) { fast=fast->next->next; slow=slow->next; if (Fast==slow) { slow=head; while (Slow!=fast) { fast=fast->next; slow=slow->next; } return slow; } } return NULL; }};
This article references: http://blog.sina.com.cn/s/blog_6f611c300101fs1l.html
leetcode--linked list cycle &linked list cycle II