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

链表的奇偶重排

https://www.nowcoder.com/practice/02bf49ea45cd486daa031614f9bd6fc3

/*
 * function ListNode(x){
 *   this.val = x;
 *   this.next = null;
 * }
 */
/**
 * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
 *
 * 
 * @param head ListNode类 
 * @return ListNode类
 */
function oddEvenList( head ) {
    // write code here
    let p1 = head
    let current = 1
    let ji = new ListNode(0)
    let currentJi = ji
    let ou = new ListNode(0)
    let currentOu = ou
    while(p1) {
        if (current % 2) { //奇
            currentJi.next = p1
            currentJi = currentJi.next
        }
        else {
            currentOu.next = p1
            currentOu = currentOu.next
        }
        p1 = p1.next
        current++
    }
    currentOu.next = null
    currentJi.next = ou.next
    return ji.next
}
module.exports = {
    oddEvenList : oddEvenList
};

全部评论

相关推荐

Lorn的意义:1.你这根本就不会写简历呀,了解太少了 2.你这些项目经历感觉真的没啥亮点啊,描述的不行,重写书写一下让人看到核心,就继续海投 注意七八月份ofer还是比较多的,越往后机会越少,抓住时机,抓紧检查疏漏,加油查看图片
点赞 评论 收藏
分享
07-07 12:25
门头沟学院 Java
程序员牛肉:你这个智邮公司做的就是那个乐山市税务系统的服务吗?
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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