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.