题目:

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.
* function ListNode(val) {
* this.val = val;
* this.next = null;
* }
*/
/**
* @param {ListNode} head
* @return {ListNode}
*/
var oddEvenList = function(head) {
if(head==null||head.next==null){
return head;
} var p=head.next.next,oHead=head,eHead=head.next,i=3,op=oHead,ep=eHead;
while(p!=null){
if(i%2!=0){
op.next=p;
op=op.next;
}else{
ep.next=p;
ep=ep.next;
}
p=p.next;
i++;
} ep.next=null;
op.next=eHead;
return oHead;
};

【链表】Odd Even Linked List的更多相关文章

  1. LeetCode 328. 奇偶链表(Odd Even Linked List)

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

  2. [Swift]LeetCode328. 奇偶链表 | Odd Even Linked List

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

  3. LeetCode 328:奇偶链表 Odd Even Linked List

    ​给定一个单链表,把所有的奇数节点和偶数节点分别排在一起.请注意,这里的奇数节点和偶数节点指的是节点编号的奇偶性,而不是节点的值的奇偶性. 请尝试使用原地算法完成.你的算法的空间复杂度应为 O(1), ...

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

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

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

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

  7. 单链表反转(Singly Linked Lists in Java)

    单链表反转(Singly Linked Lists in Java) 博客分类: 数据结构及算法   package dsa.linkedlist; public class Node<E> ...

  8. [LeetCode] 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. php PDO mysql

    php PDO写法连接mysql: $db=new PDO("mysql:host=localhost;dbname=sql","root","roo ...

  2. post同步请求

    // http://api.hudong.com/iphonexml.do?type=focus-c     //post请求中url不带请求参数,请求参数在参数HTTPBody中设置, 需要创建可变 ...

  3. 基于Verilog HDL的二进制转BCD码实现

    在项目设计中,经常需要显示一些数值,比如温湿度,时间等等.在数字电路中数据都是用二进制的形式存储,要想显示就需要进行转换,对于一个两位的数值,对10取除可以得到其十位的数值,对10取余可以得到个位的数 ...

  4. Android-项目所有文件报红色j,状态栏无法Run 'app'

    项目所有文件报红色j,不可用状态 状态栏如下: 无法 Run 'app' 无法 Debug 'app' ........... 以下操作按钮灰色的,无法点击: 解决方案: 只需要:Sync Proje ...

  5. Jenkins中执行docker命令报错

    Cannot connect to the Docker daemon. Is the docker daemon running on this host?   在配置Jenkins从Gitlab自 ...

  6. 设计模式总结(《Head First设计模式》学习总结)

    写在前面: 学习过程中不仅要熟练掌握技能,理论的消化吸收也必不可少.虽然个人更倾向于学习技术类的东西(短时间的精力投入很快就能看到成效...),但看了很多前辈的经验总结后才知道理论性的东西是绝对不能忽 ...

  7. 窗口与导航-----Selenium快速入门(十三)

    前面所讲的,大部分是WebDriver这个接口以及相关的类的使用.而本文所讲的窗口与导航,也是里面的内容,而且非常简单,目测就能学会. 一.窗口,也就是window,这里的窗口是指浏览器窗口.他的方法 ...

  8. js框操作-----Selenium快速入门(八)

    js框,就是JavaScript中的警告框(alert),确认框(confirm),提示框(prompt),他们都是模态窗口.什么是模态窗口,大家可以自行百度一下,简单说就是弹出的窗口是在最顶端的,你 ...

  9. BitAdminCore框架应用篇:(四)核心套件querySuite按钮功能

    索引 NET Core应用框架之BitAdminCore框架应用篇系列 框架演示:http://bit.bitdao.cn 框架源码:https://github.com/chenyinxin/coo ...

  10. ASP.NET 实现多页面合并一页显示

    目前业务有一个需求: 就是把多个网页合并到一个页面显示, 在实现过程中我一般使用两种方法: 利用母版页设置导航栏, 定位到每个网页; 利用用户控件( .acsx 后缀的文件), 但是有个问题就是传参比 ...