<?phpclass ListNode { public $val = 0; public $next = null; function __construct($val) { $this->val = $val; }}class Solution { /** * @param ListNode $head * @param Integer $k * @return ListNode */ public function rotateRight($head, $k) { $now = $head; $length = 1; while(!is_null($now->next)) { $now = $now->next; $length++; } // 头尾相连,生成旋转链表 $now->next = $head; // 注意这里的边界条件是: $length - ($k % $length) for ($i = 0; $i < $length - ($k % $length); $i++) { $head = $head->next; } $res = $head->next; $head->next = null; return $res; }}$k = 1;$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->rotateRight($head, $k);print_r($r);