一天一道LeetCode系列

(一)题目

Given a singly linked list, group all odd nodes together followed by

the even nodes. **Please note here we are talking about the node

number and not the value in the nodes.**

You should try to do it in place. The program should run in O(1) space

complexity and O(nodes) time complexity.

Example: Given :1->2->3->4->5->NULL, return :1->3->5->2->4->NULL.

Note: The relative order inside both the even and odd groups should

remain as it was in the input. The first node is considered odd, the

second node even and so on …

Credits: Special thanks to @DjangoUnchained for adding this problem

and creating all test cases.

Subscribe to see which companies asked this question

题目的意思大概是:根据数字编号,将奇数编号排在偶数编号之前,不能改变奇数编号和偶数编号原有的内部顺序。

思路:可以建立两个链表,奇数编号链表和偶数编号链表,遍历原链表,进行判断分类即可。

(二)代码实现

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
class Solution {
public:
    ListNode* oddEvenList(ListNode* head) {

        if(head == NULL)  return head; //原链表为空,即返回
        if(head->next == NULL) return head;//原链表只有两位,即返回
        ListNode* oddtail = head; //奇数编号链表尾
        ListNode* even = head->next; //偶数编号链表开始位
        ListNode* eventail = head->next;//偶数编号链表尾
        ListNode* p = head->next->next;
        int i;
        while(p){
            if((i & 0x1) == 1)//判断编号为奇数
            {
                oddtail->next = p;
                oddtail =  oddtail->next;
            }
            else {
                eventail->next = p;
                eventail=eventail->next;
            }
            p=p->next;
            i++;
        }
        eventail-> next = NULL; //链表尾置空
        oddtail->next = even;//将两个链表尾首相连,得到结果
        return head;
    }

};

【一天一道LeetCode】#328 Odd Even Linked List的更多相关文章

  1. [LeetCode] 328. Odd Even Linked List ☆☆☆(奇偶节点分别放一起)

    每天一算:Odd Even Linked List 描述 给定一个单链表,把所有的奇数节点和偶数节点分别排在一起.请注意,这里的奇数节点和偶数节点指的是节点编号的奇偶性,而不是节点的值的奇偶性. 请尝 ...

  2. LeetCode 328. Odd Even Linked List

    Given a singly linked list, group all odd nodes together followed by the even nodes. Please note her ...

  3. Java [Leetcode 328]Odd Even Linked List

    题目描述: Given a singly linked list, group all odd nodes together followed by the even nodes. Please no ...

  4. LeetCode 328. Odd Even Linked List C#

    Given a singly linked list, group all odd nodes together followed by the even nodes. Please note her ...

  5. (链表) leetcode 328. Odd Even Linked List

    Given a singly linked list, group all odd nodes together followed by the even nodes. Please note her ...

  6. Leetcode 328 Odd Even Linked List 链表

    Given 1->2->3->4->5->NULL, return 1->3->5->2->4->NULL. 就是将序号为单数的放在前面,而 ...

  7. <LeetCode OJ> 328. Odd Even Linked List

    328. Odd Even Linked List Total Accepted: 9271 Total Submissions: 24497 Difficulty: Easy Given a sin ...

  8. 【一天一道Leetcode】#203.Remove Linked List Elements

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 我的个人博客已创建,欢迎大家持续关注! 一天一道le ...

  9. 【一天一道LeetCode】#206. Reverse Linked List

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Reverse ...

随机推荐

  1. grab window

    #include <Windows.h> #include <iostream> using namespace std; #if 0 int CaptureAnImage(/ ...

  2. Android程序员必须掌握的知识点-多进程和多线程

    当某个应用组件启动且该应用没有运行其他任何组件时,Android 系统会使用单个执行线程为应用启动新的 Linux 进程.默认情况下,同一应用的所有组件在相同的进程和线程(称为"主" ...

  3. Android 6.0出现的init: cannot execve(‘XXX’):Permission denied问题:禁止SELINUX的权限设置

    最近在开发MTK的相关项目,需要将一些可执行文件添加到init.rc文件里去,但是开机后发现,这个bin文件没有权限不能执行,于是我就在init.rc中对相应的bin文件增加了权限.后来发现,改了也没 ...

  4. 剑指Offer——知识点储备-设计模式

    剑指Offer--知识点储备-设计模式 设计模式 设计模式的六大原则 (1)单一职责原则(有且仅有一个原因引起类的变化): (2)里氏替换(任何父类出现的地方子类都可以替换): (3)依赖倒置(依赖抽 ...

  5. 微信小程序实例-摇一摇抽奖

    概述 前面我们讲了如何开始微信小程序搭建和一些组件的介绍.微信小组件和微信小程序入门 微信小程序目录 为了更好的理解小程序和小程序开发,我们首先来看一下项目的目录. 首先看下根目录下的app.json ...

  6. 22 Notification样式设置内部按钮点击事件

    package com.exam1ple.demo1; import android.app.Activity; import android.app.NotificationManager; imp ...

  7. UE4成批处理透明材质

    项目中需要控制成批的物体的透明度,但是默认的时候他又不能是透明的,对,项目的要求就这么诡异. 然而却没有找到设置材质的BlendMode的功能,于是只有换了一种办法,物体需要透明时更换为透明材质,默认 ...

  8. SSH网上商城---需求分析+表关系分析

    SSH---小编初次接触的时候傻傻的以为这个跟SHE有什么关系呢?又是哪路明星歌手,后来才知道小编又土鳖了,原来SSH是这个样子滴,百度百科对她这样阐述,SSH即 Spring + Struts +H ...

  9. 【Unity Shaders】法线纹理(Normal Mapping)的实现细节

    写在前面 写这篇的目的是为了总结我长期以来的混乱.虽然题目是"法线纹理的实现细节",但其实我想讲的是如何在shader中编程正确使用法线进行光照计算.这里面最让人头大的就是各种矩阵 ...

  10. android布局Relative和gridLayout-android学习之旅(十六)

    Relative布局简介 相对布局的组件是由兄弟组件和父组价决定的,因此这种布局被称为相对布局. 属性设置介绍 RelativeLayout.Layoutparam中只能设置为true和false的属 ...