Faster \ slower speed pointer Application

Source: Internet
Author: User

Many leetcode questions use the speed pointer to solve the problem. The following describes the speed pointer.

 

Concept:

The step size of a fast pointer in each step is much larger than that of a slow pointer in one step. The speed of a fast pointer is usually twice that of a slow pointer. Pointer movement in the loop is usually: faster = faster. Next. Next, slower = slower. Next.

 

Application:

1. Used to find the midpoint or Median

2. used to determine whether the linked list has a ring and find the ring entry

3. The question contains: nth to last, so set the fast pointer step to N, and then the fast and slow pointers go at the same speed at the same time, and use the slow pointer to find the nth to last

 

Note::

1. Pay special attention to the parity of the linked list length.

2. If the fast pointer speed is twice the slow pointer speed, the cycle condition is: Faster. Next! = NULL & faster. Next. Next! = NULL

3. When setting a fast pointer step, consider the special case where the step value is equal to the length of the linked list.

4. If you want to delete the elements in the linked list, do not forget to record the previous element of the element to be deleted.

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.