一天一道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. 如何将dtb反编译成dts

    点击打开链接 由于device tree会将一个node的信息分布在各个文件里,查看起来很不方便,比如如下例子,ldb在三个文件中都有配置: imx6qdl-sabresd.dtsi: [plain] ...

  2. EBS总账模块与其他模块数据关联关系

    表名:GL_IMPORT_REFERENCES 说明:总账导入附加信息表 用途:用来追溯从子模块传入总账模块的明细,对于报表开发很有帮助 SQL 语句: select * from gl_je_hea ...

  3. Struts2中的struts.multipart.saveDir的配置

    <constant name="struts.multipart.saveDir" value="D:\\AsimsTemp"></const ...

  4. parcel和parcelable

    Parcel 在英文中有两个意思,其一是名词,为包裹,小包的意思: 其二为动词,意为打包,扎包.邮寄快递中的包裹也用的是这个词.Android采用这个词来表示封装消息数据.这个是通过IBinder通信 ...

  5. HTML5中 HTML列表/块/布局 韩俊强的博客

    从简单到复杂HTML5详解:每日更新关注:http://weibo.com/hanjunqiang  新浪微博! 1.HTML列表 1.有序 2.无序 3.有序star属性 4.有序无序列表 代码: ...

  6. Spark:大数据的电花火石!

    什么是Spark?可能你很多年前就使用过Spark,反正当年我四六级单词都是用的星火系列,没错,星火系列的洋名就是Spark. 当然这里说的Spark指的是Apache Spark,Apache Sp ...

  7. Fork And Join框架初探

    首先,关于Fork And Join框架的入门资料我觉得最好的是: Java线程(十一):Fork/Join-Java并行计算框架 本文参考了这篇文章 Fork/Join框架的核心类是ForkJoin ...

  8. [Error]Can't install RMagick 2.13.4. You must have ImageMagick 6.4.9 or later.

    gem 安装ruby插件的时候 出现了一个错误 Installing rmagick 2.13.4 with native extensions Gem::Installer::ExtensionBu ...

  9. Uva - 816 - Abbott's Revenge

    这个迷宫问题还是挺好玩的,多加了一个转向的问题,有些路口不同的进入方式会有不同的转向限制,这个会比较麻烦一点,所以定义结点结构体的时候需要加一个朝向dir.总体来说是一道BFS求最短路的问题.最后打印 ...

  10. Demand Side Platform

    DSP特点: DSP不是从网络媒体那里包买广告位,也不是采用CPD(Cost Per Day)的方式获得广告位:而是从广告交易平台(AdExchange)来通过实时竞价的方式获得对广告进行曝光的机会, ...