How to find the middle node of the linked list

Maintain two pointers, one is a fast pointer and the other is a slow pointer. The linked list is traversed. The fast pointer moves two units at a time and the slow pointer moves one unit at a time. When the fast pointer reaches the end point, if the number of nodes is Odd number, the slow pointer points to the middle, if the number of nodes is even, the slow pointer points to the right of the middle

class Solution {
    
    
    public ListNode findMid(ListNode start){
    
    
        ListNode left=start,right=start;
        while(right!=null&&right.next!=null){
    
    
            right=right.next;
            right=right.next;
            left=left.next;
        }
        return left;
    }
 }

Guess you like

Origin blog.csdn.net/CY2333333/article/details/108081142