题解 | #链表的奇偶重排#
链表的奇偶重排
https://www.nowcoder.com/practice/02bf49ea45cd486daa031614f9bd6fc3
<?php
/*class ListNode{
var $val;
var $next = NULL;
function __construct($x){
$this->val = $x;
}
}*/
/**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
*
*
* @param head ListNode类
* @return ListNode类
*/
function oddEvenList( $head )
{
// write code here
if($head==null || $head->next==null){
return $head;
}
$odd = $head;
$even = $head->next;
$evenHead = $even;
while($even && $even->next){
$odd->next = $even->next;
$odd = $even->next;
$even->next = $odd->next;
$even = $odd->next;
}
$odd->next = $evenHead;
return $head;
}
阿里云成长空间 794人发布