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 ...

详见:https://leetcode.com/problems/odd-even-linked-list/description/

C++:

/**
* 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||!head->next)
{
return head;
}
ListNode *pre=head;
ListNode *cur=head->next;
while(cur&&cur->next)
{
ListNode *tmp=pre->next;
pre->next=cur->next;
cur->next=cur->next->next;
pre->next->next=tmp;
cur=cur->next;
pre=pre->next;
}
return head;
}
};

参考:https://www.cnblogs.com/grandyang/p/5138936.html

328 Odd Even Linked List 奇偶链表的更多相关文章

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

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

  2. [LeetCode] Odd Even Linked List 奇偶链表

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

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

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

  4. 【Leetcode】 328. Odd Even Linked List

    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. 328. Odd Even Linked List

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

  8. 【一天一道LeetCode】#328 Odd Even Linked List

    一天一道LeetCode系列 (一)题目 Given a singly linked list, group all odd nodes together followed by the even n ...

  9. 【leetcode】328. Odd Even Linked List

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

随机推荐

  1. Shiro-工作流程

    [与Web集成] 1.Shiro 提供了与 Web 集成的支持,其通过一个ShiroFilter 入口来拦截需要安全控制的URL,然后进行相应的控制. 2.ShiroFilter 类似于如 Strut ...

  2. 2017北京ICPC C题 Graph

    #1629 : Graph 时间限制:4000ms 单点时限:4000ms 内存限制:256MB 描述 The country contains N cities numbered from 1 to ...

  3. [luoguP3565] [POI2014]HOT-Hotels(dfs)

    传送门 三点在树上距离相等的情况只有一种,就是以某一个点为中心,三个点到这个点的距离相等. 所以直接枚举每个点作为中心,dfs这个中心的子树,根据乘法原理统计答案即可. 时间复杂度 O(n2) (n ...

  4. 20180705关于mysql binlog的解析方式

    来自:https://blog.csdn.net/u012985132/article/details/74964366/ 关系型数据库和Hadoop生态的沟通越来越密集,时效要求也越来越高.本篇就来 ...

  5. 转载 字符串hash

    转载自:http://www.cnblogs.com/jiu0821/p/4554352.html 求一个字符串的hash值: •现在我们希望找到一个hash函数,使得每一个字符串都能够映射到一个整数 ...

  6. mongodb片健的选取及更改

    1.总的原则:选键的时候多考虑以下问题. 写操作是怎么样的,有多大? 系统每小时会写多少数据,每天呢,高峰期呢 那些字段是随机的,那些是增长的 读操作是怎么样的,用户在访问那些数据 数据索引做了吗?应 ...

  7. 20、Java并发性和多线程-Slipped Conditions

    以下内容转自http://ifeve.com/slipped-conditions/: 所谓Slipped conditions,就是说, 从一个线程检查某一特定条件到该线程操作此条件期间,这个条件已 ...

  8. 项目中应用到的框架和技术之一——Materialize

    一群做C++的老伙计搞前端开发,徒手写html和css应该会折寿..在网上找了半天,Materialize算是用起来很方便的一款前端界面框架.Google的Material Design看起来感觉还是 ...

  9. docker: 定时检查docker container的运行状态并发邮件报警

    首先创建一个发送邮件的bash脚本 - send_mail.sh: #!/bin/bash curl -s --user 'api:key-xxxxxxxxxxxxx' \ https://api.m ...

  10. CentOS 7: 设置时区和时间

    查看当前时区和时间 $ date $ ls -l /etc/localtime 查看所有可用时区 $ timedatectl list-timezones | grep Asia 设置时区 $ tim ...