题解 | #链表的奇偶重排#

链表的奇偶重排

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;
}

全部评论

相关推荐

点赞 评论 收藏
分享
代码飞升:别用口语,后端就写后端,前端就写前端,最后别光后悔
点赞 评论 收藏
分享
头顶尖尖的程序员:我也是面了三四次才放平心态的。准备好自我介绍,不一定要背熟,可以记事本写下来读。全程控制语速,所有问题都先思考几秒,不要急着答,不要打断面试官说话。
点赞 评论 收藏
分享
在瑞幸干两年,奥特曼都得闪灯
不知名的牛友:奥特曼每天只上3分钟班
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务