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

 /**
  * Definition for singly-linked list.
  * public class ListNode {
  *     int val;
  *     ListNode next;
  *     ListNode(int x) { val = x; }
  * }
  */
 public class Solution {
     public ListNode oddEvenList(ListNode head) {
         if(head==null || head.next==null) {
             return head;
         }

         ListNode odd = head;
         ListNode even = head.next;
         ListNode evenHead = head.next;

         while(even != null && even.next != null) {
             odd.next = even.next;
             odd = odd.next;
             even.next = odd.next;
             even = odd.next;
         }

         odd.next = evenHead;
         return head;
     }
 }

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

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

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

  2. Java [Leetcode 328]Odd Even Linked List

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

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

  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 1->2->3->4->5->NULL, return 1->3->5->2->4->NULL. 就是将序号为单数的放在前面,而 ...

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

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

  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 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...

  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. SQL组合查询的存储过程写法

    最进一个项目 里面有个查询的功能,它是进行组合查询的, 而且用的是存储过程写.写这样的存储过程,需要注意单引号的使用,请看本人下面的例子,假如你以后写的话 记得注意写就行: create proc s ...

  2. myeclipse中的weblogic 服务未正常关闭问题解决。

    myeclipse中的weblogic 服务若未正常关闭,直接启动服务会出现weblogic服务被锁. 正确操作:应该将相应的服务关掉然后重启 打开任务管理器,进入进程找到javaw,将进程结束.然后 ...

  3. iOS 汉字拼音

    获取汉字的拼音 #import <Foundation/Foundation.h> @interface NSString (Utils) /** *  汉字的拼音 * *  @retur ...

  4. SpringMVC中使用Cron表达式的定时器

    SpringMVC中使用Cron表达式的定时器 cron(定时策略)简要说明 顺序: 秒 分 时 日 月 星期 年份 (7个参数,空格隔开各个参数,年份非必须参数) 通配符: , 如果分钟位置为* 1 ...

  5. js中toString和valueOf方法的区别

    toString 方法 返回对象的字符串表示形式. 语法:objectname.toString([radix]) objectname 必需.要为其搜索字符串表示形式的对象. radix 可选.为将 ...

  6. 第三篇T语言实例开发,图色操作

    ---恢复内容开始--- 图色的基本操作 1.找颜色色命令的基本操作 坐标点取色:获取指定坐标点的颜色 区域找色:在指定区域里找某一个颜色 模糊找色:在指定区域里找某一个颜色,可以设置相似度 多点找色 ...

  7. PHP 登录

    <?php// 本类由系统自动生成,仅供测试用途namespace Home\Controller;use Think\Controller;use Org\Util\Rbac; use Thi ...

  8. [WARNING]考前必读?!

    考试策略很重要. 1)             认真读懂每道题,把题意搞明白,把样例算出来,把题目要求的细节标出. 2)             推出每道题的模型,确定每道题的策略,是部分分还是要全部 ...

  9. 反射【类Class、成员变量Field、方法Method】

    Class<?> c = Class.forName("main.Main$MyClass"); //要包名+类名 Object obj = c.newInstance ...

  10. TimeQuest 静态时序分析 基本概论

    静态时序分析 基本概念  [转载] 1.   背景 静态时序分析的前提就是设计者先提出要求,然后时序分析工具才会根据特定的时序模型进行分析,给出正确是时序报告. 进行静态时序分析,主要目的就是为了提高 ...