LeetCode高频面试60天打卡日记Day23

Day23(链表中间节点—快慢指针)

在这里插入图片描述

/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode(int x) { val = x; }
 * }
 */
class Solution {
    
    
    public ListNode middleNode(ListNode head) {
    
    
        if(head==null){
    
    
            return null;
        }
        ListNode fast = head;
        ListNode slow = head;
        while(fast!=null && fast.next!=null){
    
    
            fast = fast.next.next;
            slow = slow.next;
        }
        return slow;
    }
}

猜你喜欢

转载自blog.csdn.net/YoungNUAA/article/details/105056733