# Leetcode first click _ Linked List Cycle II

Source: Internet
Author: User

This question is a little interesting. I know the answer and find it. It's easy to do. At first, I thought about how to use another pointer after the meeting, and then push it out after satisfying the relationship. In fact, this is not necessary. It is a very simple mathematical relationship. Let me draw a picture to illustrate it.

S1 represents the length before the linked list enters the ring. a represents the length of the pointer in the ring when two pointers encounter each other. S2 represents the circumference of the ring, then, according to the conditions, the distance between the two-step pointer is twice the distance between the two-step pointer. We get the formula:

(S1 + a) * 2 = S1 + S2 +

Simplified to get <喎?http: www.bkjia.com kf ware vc " target="_blank" class="keylink"> VcD4KPHA + forward + vLSjrLu31tDKo8/forward + forward/forward + YWO0 + forward + CjxwcmUgY2xhc3M9 "brush: java;"> class Solution {public: ListNode * detectCycle) {if (! Head |! Head-> next) return NULL; ListNode * a = head-> next, * B = head-> next; while (! = B & a & B-> next) {a = a-> next; B = B-> next;} if (! = B) return NULL; a = head; while (! = B) {a = a-> next; B = B-> next;} return ;}};

Related Keywords:
Related Article

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.