The application and principle of fast and slow pointers (in the linked list)

Fast and slow pointer application
The concept of fast and slow pointers

Use two pointers with different speeds in a linked list to solve some practical problems in the linked list.

The main application scenarios of this method:

  • Determine whether there is a ring in a linked list
  • If there is a ring in a linked list, find the position where the ring starts
  • Find the kth from the bottom in the linked list

I only met the third one, so write the third one first


Find the kth from the bottom in the linked list

Sword refers to Offer 22. The kth node from the bottom in the linked list

Input a linked list, and output the kth node from the bottom of the linked list. In order to conform to the habits of most people, this question starts counting from 1, that is, the end node of the linked list is the first node from the bottom.

For example, there is a linked list 6of nodes, the nodes start from scratch, their values followed 1、2、3、4、5、6. The list of the penultimate 3node is the value 4of the node.

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
class Solution {
public:
    ListNode* getKthFromEnd(ListNode* head, int k) {

int cnt=0;
ListNode*temp1=head;//快指针
ListNode*temp2=head;//慢指针
while(cnt!=k)
{
    temp1=temp1->next;
    cnt++;
}
while(temp1)
{
temp1=temp1->next;
temp2=temp2->next;
}return temp2;
    }
};

The principle is to make the fast pointer go k-1 step first, then make the fast pointer go one step, the slow pointer go one step, and finally when the fast pointer goes to null, the slow pointer is the kth from the bottom;

The following content has not been encountered because there are fewer questions, if you encounter it, add additional code

Write out the principle for the time being, so as not to forget it in the future

Determine whether a linked list has a ring

You can set two pointers fast, slow, fast pointers to move two grids at a time, and slow pointers to move one grid at a time.

If the chain does not contain a ring, then the fast pointer will always point to null at the end; if it contains a ring, the fast pointer and the slow pointer will meet at some point == (if it contains a ring, it will definitely meet, because it can be assumed that slow Do not move, and fast only takes one step, so as long as there is a ring, you can meet)==

img


Determine the length of a ring in a linked list

The premise is that the linked list has a ring.

It can be seen from the above that if there is a ring, then the fast pointer and the slow pointer will meet. If they do, then one of the pointers will move one step at a time, and the other will not move, and record the number of spaces the two pointers walk when they meet. It is the length of the ring.

Find the entry node

Assuming that the length of the ring in the linked list is already known, then the fast pointer and the slow pointer are initialized as the head pointer, and the fast pointer takes n-1 steps more than the slow pointer (n is the length of the ring);

When the two meet again, it means that the entry node is reached;

==Reference from: Knowing==

Refer to: B station video, I personally think the video is easy to understand;

(https://www.bilibili.com/video/BV1R7411G78m?from=search&seid=17241189924549668943) video, I personally think the video is easy to understand;

Guess you like

Origin blog.csdn.net/weixin_45929885/article/details/114235797