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. 通过ros节点发布Twist Messages控制机器人--10

    原创博客:转载请表明出处:http://www.cnblogs.com/zxouxuewei/ 1.到目前为止,我们已经从命令行移动机器人,但大多数时间你将依靠一个ros节点发布适当的Twist消息. ...

  2. html部分---样式表,选择器;

    <1.内联样式,优点:控制精确,缺点:代码重用性差,页面代码乱.> <div style="background-color:#0F0"></div& ...

  3. Dwarves (有向图判环)

    Dwarves 时间限制: 1 Sec  内存限制: 64 MB提交: 14  解决: 4[提交][状态][讨论版] 题目描述 Once upon a time, there arose a huge ...

  4. kuangbin_ShortPath J (POJ 1511)

    其实虽然一开始有被这个题的8000MS 和 256MB限制又被吓到 但是严格来说跟之前的POJ 3268是一样的做法只是数据大了点 但是问题就出在数据大了点上 其实严格来说也不大 1e6 数组加起来大 ...

  5. opencv矩阵总结

    OpenCV 矩阵操作 CvMat 转自:http://hi.baidu.com/xiaoduo170/blog/item/10fe5e3f0fd252e455e72380.html 每回用矩阵都要查 ...

  6. 将caffe训练时loss的变化曲线用matlab绘制出来

    1. 首先是提取 训练日志文件; 2. 然后是matlab代码: clear all; close all; clc; log_file = '/home/wangxiao/Downloads/43_ ...

  7. http协议传输二进制数据以及对输入流(php://input)和http请求的理解

    1.index.php <?php $data=file_get_contents('./a.jpg'); $opts = array('http' => array( 'method' ...

  8. apk反编译生成程序的源代码和图片、XML配置、语言资源等文件

    Android应用的UI越来越漂亮,遇到喜欢的我们可以通过反编译,得到应用的源代码借鉴下别人的思想. 具体步骤: 1.下载 apktool 下载地址:https://code.google.com/p ...

  9. How to calculate a good InnoDB log file size

    Peter wrote a post a while ago about choosing a good InnoDB log file size.  Not to pick on Peter, b ...

  10. NLTK中的词性

    NOUN n,VERB v ,ADJ a, ADV r, ADJ_SAT s     NOUN: [('s', ''), ('ses', 's'), ('ves', 'f'), ('xes', 'x' ...