1. Middle of the Linked List题目链接876. Middle of the Linked List题目分析返回一个链表中最中间的元素。思路先全部塞入数组,再根据长度/2得到中间元素的下标,再返回。最终代码<?php/** * Definition for a singly-linked list. * class ListNode { * public $val = 0; * public $next = null; * function __construct($val) { $this->val = $val; } * } */class Solution { function middleNode($head) { $items = [$head]; while($head){ $items[] = $head; $head = $head->next; }; return $items[ceil(count($items)/2)]; }}若觉得本文章对你有用,欢迎用爱发电资助。