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. vmware-workstation-11中centos-6.6安装

    排版比较乱,参见 https://www.zybuluo.com/Jpz/note/144303 VMware Workstation 11中CentOS 6.6安装 Linux开发环境配置 目录 V ...

  2. HOG参数简介及Hog特征维数的计算(转)

    HOG构造函数 CV_WRAP HOGDescriptor() :winSize(64,128), blockSize(16,16), blockStride(8,8),      cellSize( ...

  3. xmind的第二天笔记

  4. cdoj 482 优先队列+bfs

    Charitable Exchange Time Limit: 4000/2000MS (Java/Others)     Memory Limit: 65535/65535KB (Java/Othe ...

  5. POJ 2251 Dungeon Master --- 三维BFS(用BFS求最短路)

    POJ 2251 题目大意: 给出一三维空间的地牢,要求求出由字符'S'到字符'E'的最短路径,移动方向可以是上,下,左,右,前,后,六个方向,每移动一次就耗费一分钟,要求输出最快的走出时间.不同L层 ...

  6. UVALive-4670 Dominating Patterns(AC自动机)

    题目大意:找出出现次数最多的模式串. 题目分析:AC自动机裸题. 代码如下: # include<iostream> # include<cstdio> # include&l ...

  7. STM32时钟系统

    一.在STM32中,有五个时钟源,为HSI.HSE.LSI.LSE.PLL. ①HSI是高速内部时钟,RC振荡器,频率为8MHz. ②HSE是高速外部时钟,可接石英/陶瓷谐振器,或者接外部时钟源,频率 ...

  8. lua堆栈操作常用函数学习二

    /* ** basic stack manipulation */ LUA_API int  <strong> (lua_gettop) (lua_State *L);  </str ...

  9. linearlist和linkedlist的区别 待整理

    线性表在内存中是一块连续的存储空间:如:一个表中的内容是:[1,2,3]则它在内存中可能是如下存储的:1 2 3 优点:查找       通过这个结构可以看出,只要知道了第一个元素在内存中所在的位置. ...

  10. SSRS入门相关笔记

    1.SSRS Server 的地址的查看及设置:打开 开始->程序-> Microsoft SQL Server 2012/2014 -> Configuration Tools - ...