<?phpClass ListNode { public $val = 0; public $next; public function __construct($val) { $this->val = $val; }}class Solution { public function reverseKGroup(ListNode $head, $k) { $node = $head; for($i = 0; $i < $k; $i++) { if (!$node) return $head; $node = $node->next; } $newHead = $this->reverse($head, $node); $head->next = $this->reverseKGroup($node, $k); return $newHead; } private function reverse(ListNode $start, ListNode $end) { $pre = null; while ($start !== $end) { $tmp = $start->next; $start->next = $pre; $pre = $start; $start = $tmp; } return $pre; }}$head = new ListNode(1);$head->next = new ListNode(2);$head->next->next = new ListNode(3);$head->next->next->next = new ListNode(4);$head->next->next->next->next = new ListNode(5);$cls = new Solution();$r = $cls->reverseKGroup($head, 3);print_r($r);