Fast and Slow Pointer Method for Finding Intermediate Nodes in Linked List

When doing LeetCode question 876, I encountered a very novel problem-solving method, the fast and slow pointer method.

This question is to find the middle node of a singly linked list.

Fast and slow pointer method: two pointers, the fast pointer takes two steps at a time, and the slow pointer takes one step at a time. When the fast pointer reaches the end, the slow pointer points to the middle node.

Extension: As long as the distance between the fast and slow pointers is controlled, the node at any position can be obtained with O(n) time complexity.

Guess you like

Origin blog.csdn.net/m0_47540684/article/details/128523313