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.

========

重排链表,链表的奇数元素都排在偶数元素前面,

而且奇数间的相对位置不变,偶数间的相对位置不变.

思路:

两个假的dummy节点,分别按照  尾插法链接原始,

最后将偶数链表接在奇数链表的后边即可.

/**
* 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==nullptr || head->next==nullptr) return head;
ListNode dummy1(-);
ListNode dummy2(-);
ListNode *h1 = &dummy1;
ListNode *h2 = &dummy2;
int k = ;
while(head){
if(k%==){
h1->next = head;
h1 = h1->next;
}else{
h2->next = head;
h2 = h2->next;
}
k++;
head = head->next;
}
h2->next = dummy1.next;
h1->next = nullptr;
return dummy2.next;
}
};

328. Odd Even Linked List的更多相关文章

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

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

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

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

  3. LeetCode 328. Odd Even Linked List

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

  4. 328. Odd Even Linked List——多利用fake_head

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

  5. Java [Leetcode 328]Odd Even Linked List

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

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

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

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

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

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

  9. 【Leetcode】 328. Odd Even Linked List

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

随机推荐

  1. indexOf()的用法

    indexOf()的用法:返回字符中indexof(string)中字串string在父串中首次出现的位置,从0开始!没有返回-1:方便判断和截取字符串! indexOf()定义和用法indexOf( ...

  2. java正则表达式 非捕获组详解

    这几天看了下正则表达式,对非捕获组(non-capturing)进行下总结.主要总结 1个 + 2组  一共5个.(?:X) (?=X) (?<=X) (?!X) (?<!X) 一.先从( ...

  3. spring二级缓存的ehcache 的 配置文件

    <ehcache> <!-- Sets the path to the directory where cache .data files are created. If the p ...

  4. LNMP环境搭建(discuz论坛)

    一.操作系统级环境及软件版本 操作系统:CentOS release 6.5 (Final)minimal 内核版本:2.6.32-431.el6.x86_64 MySQL版本:MySQL-5.6.2 ...

  5. 新浪代码部署手册 git管理工具

    目前新浪云上的应用支持通过Git和SVN来部署代码. Git仓库地址 https://git.sinacloud.com/YOUR_APP_NAME SVN仓库地址 https://svn.sinac ...

  6. Oracle学习系列2

    SQL语法练习: 1,选择部门30中的所有员工 select * from emp where deptno=30; 2,列出办事员的姓名,编号和部门编号 select ename, empno, d ...

  7. 黑马程序员——JAVA基础之异常处理机制

    ------- android培训.java培训.期待与您交流! ---------- 异常:就是程序在运行时出现不正常情况. 异常由来:问题也是现实生活中一个具体的事物,也可以通过java的类的形式 ...

  8. Hadoop SPARK 环境搭建

    http://www.linuxidc.com/Linux/2015-02/113486.htm http://www.cnblogs.com/lijingchn/p/5574476.html htt ...

  9. word2010插入奇数页 使奇偶页不同的问题

    word2010版本,设计奇偶页不同时,遇到一个问题,就是在第一章最后插入分节符——奇数页时,会导致整个奇数页都为1.原因:①首先每一章的末尾都应该有分页符或者奇数页等分节符来分开每一章:②点击页码可 ...

  10. Expect安装方法

    Expect安装方法 http://bluethink.iteye.com/blog/1079044 Tcl脚本SSHITeyeUnix  Expect是基于Tcl语言的一种脚本语言,其实无论是交互还 ...