876. Middle of the Linked List

/**
 * Definition for a singly-linked list.
 * class ListNode {
 *     public $val = 0;
 *     public $next = null;
 *     function __construct($val = 0, $next = null) {
 *         $this->val = $val;
 *         $this->next = $next;
 *     }
 * }
 */
class Solution {

    /**
     * @param ListNode $head
     * @return ListNode
     */
    function middleNode($head) {
        $fast = $head;
        while ($fast && $fast->next){
            $fast = $fast->next->next;
            $head = $head->next;
        }
        return $head;
    }
}

https://leetcode.com/problems/middle-of-the-linked-list/solutions/3612036/6-lines

Last updated