[Leetcode]93. Linked List Cycle II find the starting node of a ring in a linked list

Source: Internet
Author: User

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

Note:do not modify the linked list.

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

Subscribe to see which companies asked this question

Solution: The problem is the title linked list cycle to determine if the chain list has a ring extension, which requires the return ring starting position or null. As above, we still first use the fast and slow pointer method to determine whether the chain list exists ring, if there is two pointer to the first intersection of the position. It can be proved that this position is the same as the starting position of the loop and the number of nodes of the chain head node from the beginning of the ring. So all we have to do is set the two pointers to start with the head node and the intersection node, step forward, and the first intersection of the two pointers is the starting node of the ring.
/** Definition for singly-linked list. * struct ListNode {* int val; * ListNode *next; * ListNode (int x) : Val (x), Next (NULL) {}}; */classSolution { Public: ListNode*detectcycle (ListNode *head) {        if(head = = NULL | | head->next = = NULL)returnNULL; ListNode*slow = head, *fast =Head;  while(Fast->next! = NULL && Fast->next->next! = null) {//determine if there is a ringslow = slow->Next; Fast= fast->next->Next; if(slow = = fast) Break; }        if(Fast->next = = NULL | | fast->next->next = = NULL)returnNULL; Slow=Head;  while(Slow! =fast) {Slow= slow->Next; Fast= fast->Next; }        returnslow; }};

Description: Assume that there are N1 nodes outside the ring, the ring has N2 nodes, when the slow pointer walks N1 step to the entry node of the ring, the fast pointer goes 2n1 step, at this point in the ring position is the distance from the entrance node has (2N1-N1)%N2 nodes, that is, in front of the entrance node N1%N2 nodes. That is the equivalent of fast pointer behind slow pointer N2-N1%N2 nodes. The speed pointer is 1 relative to the slow pointer, so the two pointers will meet after the N2-N1%N2 step, when the slow pointer moves forward from the entry node N2-N1%N2 nodes. Therefore, the distance between the intersection point and the entry node is n2-(N2-N1%N2) =n1%n2 node. At this point the slow pointer starts at the beginning of the node, and the fast pointer starts at the intersection, each moving forward at 1, and the first intersection must be the entry node. The fast pointer either took the N1%N2 step (n1<n2), or walked N1%n2+kn2 (k is a positive integer) (n1>n2 case, that is, the fast pointer in the ring after a few laps and then walk n1%n2 step).

For more questions about loops in a single-linked list, refer to http://www.cnblogs.com/hiddenfox/p/3408931.html, http://www.cnblogs.com/wuyuegb2312/p/3183214.html

[Leetcode]93. Linked List Cycle II find the starting node of a ring in a linked list

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.