328. Odd Even Linked List
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的更多相关文章
- <LeetCode OJ> 328. Odd Even Linked List
328. Odd Even Linked List Total Accepted: 9271 Total Submissions: 24497 Difficulty: Easy Given a sin ...
- [LeetCode] 328. Odd Even Linked List ☆☆☆(奇偶节点分别放一起)
每天一算:Odd Even Linked List 描述 给定一个单链表,把所有的奇数节点和偶数节点分别排在一起.请注意,这里的奇数节点和偶数节点指的是节点编号的奇偶性,而不是节点的值的奇偶性. 请尝 ...
- LeetCode 328. Odd Even Linked List
Given a singly linked list, group all odd nodes together followed by the even nodes. Please note her ...
- 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 ...
- Java [Leetcode 328]Odd Even Linked List
题目描述: Given a singly linked list, group all odd nodes together followed by the even nodes. Please no ...
- 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 ...
- 【一天一道LeetCode】#328 Odd Even Linked List
一天一道LeetCode系列 (一)题目 Given a singly linked list, group all odd nodes together followed by the even n ...
- (链表) leetcode 328. Odd Even Linked List
Given a singly linked list, group all odd nodes together followed by the even nodes. Please note her ...
- 【Leetcode】 328. Odd Even Linked List
Given a singly linked list, group all odd nodes together followed by the even nodes. Please note her ...
随机推荐
- 137. Single Number II
Given an array of integers, every element appears three times except for one. Find that single one. ...
- C++ Primer : 第十二章 : 文本查询程序
C++ Primer书上这个例子讲的很不错,写写帮助自己理解标准库和智能指针. .h 文件内容 #include <fstream> #include <iostream> # ...
- hdu3308 线段树——区间合并
更新一个点: 求某个区间的最长连续上升序列: 链接:http://acm.hdu.edu.cn/showproblem.php?pid=3308 #include <cstdio> #in ...
- URAL(timus)1709 Penguin-Avia(并查集)
Penguin-Avia Time limit: 1.0 secondMemory limit: 64 MB The Penguin-Avia airline, along with other An ...
- phpwind8.7升级9.0.1过程(二)8.7正式升级9.0
首先备份 1. 给本地做备份将网站根目录下面的所有文件先做备份. 2. 给本地数据库做备份 3. 了解升级的具体过程 phpwind8.7升级到9.0需要首先升级到phpwind9.0的原生版本即:2 ...
- C++@重载函数
关于重载详细分析参考: http://www.cnblogs.com/skynet/archive/2010/09/05/1818636.html 内部机制涉及重载函数如何解决命名冲突,调用匹配的问题 ...
- [CTSC 2012][BZOJ 2806]Cheat
真是一道好题喵~ 果然自动机什么的就是要和 dp 搞基才是王道有木有! A:连 CTSC 都叫我们搞基,果然身为一个程序猿,加入 FFF 团是我此生最明智的选择.妹子什么闪边去,大家一起来搞基吧! Q ...
- DNS与DSN
dns是域名系统的缩写 dsn在一些专业书籍中是数据源的缩写
- JavaScript的事件绑定及深入
事件绑定分为两种:一种是传统事件绑定(内联模型,脚本模型),一种是现代事件绑定 (DOM2 级模型).现代事件绑定在传统绑定上提供了更强大更方便的功能. 一.传统事件绑定的问题传统事件绑定有内联模型 ...
- lua for循环
<转自网络,仅供学习> for循环是一个循环控制结构,可以有效地编写需要执行的特定次数的循环. 语法 Lua编程语言的for循环的语法是: for init,max/min value, ...