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

分析:题意为给一个单链表,将所有偶数位节点组合起来后面接上奇数位节点,要求算法的时间复杂度为O(n)空间复杂度为O(1),注意:奇数位和偶数位组合内相对顺序不变,第一个节点被认为是偶节点,第二个节点为奇节点。。。

代码:

/**
* 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==NULL || head->next==NULL) return head; ListNode *odd,*even,*even_head;
odd=head;
even=head->next;
even_head=head->next; while(even->next){
odd->next=even->next;
odd=odd->next;
if(odd->next){
even->next=odd->next;
even=even->next;
}
else{
even->next=NULL;
}
}
odd->next=even_head;
return head;
}
};

  

leetcode: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

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

  4. leetcode:234. Palindrome Linked List

    这个题目非常好.http://blog.csdn.net/u012249528/article/details/47124771给出了三种解法,其中前两个是不满足条件的,不过具有参考价值: 第一种办法 ...

  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 C#

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

  7. Python解Leetcode: 725. Split Linked List in Parts

    题目描述:给定一个单链表,写一个函数把它分成k个单链表.分割成的k个单链表中,两两之间长度差不超过1,允许为空.分成的k个链表中,顺序要和原先的保持一致,比如说每个单链表有3个结点,则第一个单链表的结 ...

  8. Leetcode 328 Odd Even Linked List 链表

    Given 1->2->3->4->5->NULL, return 1->3->5->2->4->NULL. 就是将序号为单数的放在前面,而 ...

  9. Leetcode:206. Reverse Linked List

    这题直接ac掉 class Solution { public ListNode reverseList(ListNode head) { ListNode prev = null; while(he ...

随机推荐

  1. java 邮箱验证公共方法

  2. 亚马逊 在线测试题目 amazon

    分析:其实就是求矩形中某一个点到其他点的距离加权最小 方法一: 对每一个点求其到其他点的加权距离,然后比较最小.由于有M*N个点,对每一个点求加权距离是O(M*N)的,所以整体时间复杂度是O(M*M* ...

  3. JS This关键字

    http://www.ruanyifeng.com/blog/2010/04/using_this_keyword_in_javascript.html http://www.cnblogs.com/ ...

  4. zero to one:创业秘籍并不存在,因为任何创新都是新颖独特的,任何权威都不可能具体规定如何创新

    彼得·蒂尔(Peter Thiel)的新作<从0到1>从预售开始就占据美国亚马逊排行榜第一名的位置,被一批创业家和企业家评为“迄今为止最好的商业书”.这是一本关于如何创建创新公司的书,主要 ...

  5. JS获取节点方法

    1. 通过顶层document节点获取:(1) document.getElementById(elementId):该方法通过节点的ID,可以准确获得需要的元素,是比较简单快捷的方法.如果页面上含有 ...

  6. 有关npm rum的3个简洁技巧

    [编者按]本文作者为来自 MongoDB 的 NodeJS 工程师 Valeri Karpov.Valeri 专注于维护常见的 Mongoose ODM,是<Professional Angul ...

  7. winform中的时间轴控件

    我现在做的项目遇到一个需求,就是有没有类似的控件: 我要实现的功能是:播放录像. 某个时间段内假如有2个录像,这个坐标表示的是时间,假如我现在拖动时间轴,拖到第一个录像里面开始播放第一个录像,拖到2个 ...

  8. sql脚本太大无法打开的解决办法

    在sqlcmd中执行脚本文件的方法有2种: 方法1.在DOS中,可以调用sqlcmd命令,并用选项-i传入想要执行的文件名: sqlcmd -S "这里改成你的服务器名称" -U  ...

  9. To Use Ubuntuubunt

    1.rename PC; modify the /etc/hostname. 2.Use root account; 1.set the password for root account: sudo ...

  10. log4net 一分钟上手

      1. 首先从apache网站下载log4net, http://logging.apache.org/log4net/download_log4net.cgi .我下的是最新版本 log4net- ...