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

Solution 1):

 /**
* Definition for singly-linked list.
* public class ListNode {
* public int val;
* public ListNode next;
* public ListNode(int x) { val = x; }
* }
*/
public class Solution {
public ListNode OddEvenList(ListNode head) {
if(head==null)
{
return head;
}
ListNode odd = head;
ListNode even = head.next;
while(even!=null&&even.next!=null)
{
ListNode oddNext = odd.next;
ListNode evenNext = even.next;
even.next = evenNext.next;
odd.next = evenNext;
evenNext.next = oddNext;
odd = odd.next;
even = even.next; }
return head;
}
}

 Solution 2):

 1->2->3->4->5->6->7    to odd: 1->3->5  and even 2->4->6;

then return odd ->even;

 public class Solution {
public ListNode OddEvenList(ListNode head) {
if(head==null)
{
return head;
}
ListNode odd = head;
ListNode even = head.next;
ListNode evenHead = even;
while(even!=null&&even.next!=null)
{
odd.next = even.next;
odd = odd.next;
even.next = odd.next;
even = even.next;
}
odd.next = evenHead;
return head;
}
}

    

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

  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

    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. a5站长论坛和s8站长论坛-网上兼职做任务赚钱的两大网站

     1.什么是做任务赚钱? 简而言之,就是你做别人不在行而你在行的技术 ,如 图片美化 网站建设 网站修改 网站推广等网络业务. 2.任务赚钱有什么特点? 完全免费,你付出的是技术和时间,完全免费,不过 ...

  2. building android/ubuntu-touch on 32bit machine

    host C: libhost <= build/libs/host/CopyFile.c host StaticLib: libhost (/media/linux/1/touch/expor ...

  3. Promise初体验

    想通过回调函数做一个动画效果:三个小球依次运动,第一个小球运动到指定位置后回调第二个小球运动,依次类推,效果如图所示: 到第三个小球到达指定位置再回调,让第二个小球往回移动,直到最后一个小球回到原位: ...

  4. java基础(一)对象

    对象的创建 Test test = new Test(); Test test = new Test("a"); 其实,对象被创建出来时,对象就是一个对象的引用,这个引用在内存中为 ...

  5. restful Api 风格契约设计器:Swagger-editor和swagger-ui的应用

    swagger-editor的安装 swagger-editor应用的yaml语法,有定义变量和数据结构,不明白可以参考其示例 安装步骤: 下载swagger-editor git地址 运行npm r ...

  6. ipc 入侵步骤

    第一步:建立IPC隧道net use \\10.56.204.186\IPC$ "密码" /user:"用户"     第二步:映射对方c盘到本地z盘net u ...

  7. 队列Queue FIFO先进先出 栈Stack FILO先进后出

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...

  8. python自动化开发-2

    1.python的数据类型之列表 列表是Python开发语言中最常见的数据类型之一,通过列表可以实现对数据的增删改等常用操作. 列表的定义:例子 names = ["Lucy",& ...

  9. 模仿jQuery的filter方法

    对这类方法挺感兴趣的,因为方法的回调函数的返回值和jQuery变量好像没有什么关系.看了filter方法的源代码后,我就模仿了这个方法,自定义两个jQuery方法:some和every,类似于ES5新 ...

  10. [转]解决VS2008 开发Windows Mobile 项目生成速度慢的问题

    最近用VS2008开发Windows Mobile程序,使用C#..NET Compact Framework,发现项目生成速度比较慢.用VS2008打开项目后,开始一段时间生成速度还能忍受,时间一长 ...