876. Middle of the Linked List

快慢指针求链表中间值,奇数时fast.next==null,偶数时fast==null

public ListNode middleNode(ListNode head) {
    ListNode fast = head, slow = head;
    while (fast != null && fast.next != null) {
        fast = fast.next.next;
        slow = slow.next;
    }
    return slow;
}

results matching ""

    No results matching ""