一天一道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. linux中probe函数中传递的参数来源(上)

    点击打开链接 上一篇中,我们追踪了probe函数在何时调用,知道了满足什么条件会调用probe函数,但probe函数中传递的参数我们并不知道在何时定义,到底是谁定义的,反正不是我们在驱动中定义的(当然 ...

  2. cassandra 监控方案评估

    摘要 最开始做cassandra monitor 方案的选型时,主要是从cassandra 本身入手,后来发现cassandra运行在JVM上,所有的metrics都是通过JMX 暴露出来.所以又可以 ...

  3. tomcat启动批处理——setclasspath.bat

    除了上面两个批处理,还有一个比较重要的脚本,即是setclasspath.bat,它主要负责寻找.检查JAVA_HOME和JRE_HOME两个变量. ************************* ...

  4. ghmm在 Linux 上安装

    ghmm在 Linux 上安装 http://ghmm.sourceforge.net/documentation.html http://www.ghmm.org http://www.comp.l ...

  5. Afinal加载网络图片及下载文件使用方法

    Afinal快速开发框架使用起来非常方便,下面将讲解如何利用Afinal加载网络图片及下载文件: 先看效果图: 注意:使用Afinal前需添加Afinal的jar,可以在这里下载:http://dow ...

  6. Java学习之控制跳转语句

    控制跳转语句 控制跳转语句: (1)break:中断的意思 A:用在循环和switch语句中,离开此应用场景无意义. B:作用 a:跳出单层循环 b:跳出多层循环,需要标签语句的配合 (2)conti ...

  7. 在ubuntu上部署一个samba服务器

    今天公司装了一天新电脑,准备把它装成服务器,于是为了方便开发的使用,我在上面部署了一个samba,用来实现window和linux的联系: 具体步骤,我详细的查看了百度,高手云集,以下就是总结了网友的 ...

  8. 安卓仿QQ红包领取详情界面动画

    为了能清楚的看到这个效果,本人不惜几次花费重金给众群叼发放红包,来查看红包领取详情界面的动画效果,QQ效果如图: 图中我们可以看到,动画处的头像和文字是一起的,即同时并且是整体,注意,是整体进行缩放的 ...

  9. Android开发学习之路--Content Provider之初体验

    天气说变就变,马上又变冷了,还好空气不错,阳光也不错,早起上班的车上的人也不多,公司来的同事和昨天一样一样的,可能明天会多一些吧,那就再来学习android吧.学了两个android的组件,这里学习下 ...

  10. 03_NoSQL数据库之Redis数据库:list类型

     lists类型及操作 List是一个链表结构,主要功能室push,pop.获取一个范围的所有值等等,操作中key理解为链表的名字.Redis的list类型其实就是一个每个元素都是string类型 ...