leetcode: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
.
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的更多相关文章
- [LeetCode] 328. Odd Even Linked List ☆☆☆(奇偶节点分别放一起)
每天一算:Odd Even Linked List 描述 给定一个单链表,把所有的奇数节点和偶数节点分别排在一起.请注意,这里的奇数节点和偶数节点指的是节点编号的奇偶性,而不是节点的值的奇偶性. 请尝 ...
- 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
Given a singly linked list, group all odd nodes together followed by the even nodes. Please note her ...
- leetcode:234. Palindrome Linked List
这个题目非常好.http://blog.csdn.net/u012249528/article/details/47124771给出了三种解法,其中前两个是不满足条件的,不过具有参考价值: 第一种办法 ...
- 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 C#
Given a singly linked list, group all odd nodes together followed by the even nodes. Please note her ...
- Python解Leetcode: 725. Split Linked List in Parts
题目描述:给定一个单链表,写一个函数把它分成k个单链表.分割成的k个单链表中,两两之间长度差不超过1,允许为空.分成的k个链表中,顺序要和原先的保持一致,比如说每个单链表有3个结点,则第一个单链表的结 ...
- Leetcode 328 Odd Even Linked List 链表
Given 1->2->3->4->5->NULL, return 1->3->5->2->4->NULL. 就是将序号为单数的放在前面,而 ...
- Leetcode:206. Reverse Linked List
这题直接ac掉 class Solution { public ListNode reverseList(ListNode head) { ListNode prev = null; while(he ...
随机推荐
- YEBIS
在往项目里加yebis做各种后处理 就是看起来很高大上的各种味精 我又热!泪!盈!眶!了 压缩包解开 有各种文档 恩哼~ 大概看了下找到sample build.... 直接就success了.... ...
- 使用命令行进行 VS单元测试 MSTest
测试 指定的方法 "D:\Program Files (x86)\Microsoft Visual Studio 9.0\Common7\IDE\MSTest.exe" /test ...
- VC++之GetLastError()使用说明
VC中GetLastError()获取错误信息的使用 在VC中编写应用程序时,经常需要涉及到错误处理问题.许多函数调用只用TRUE和FALSE来表明函数的运行结果.一旦出现错误,MSDN中往往会指出请 ...
- CSS预处理器实践之Sass、Less大比拼[转]
什么是CSS预处理器? CSS可以让你做很多事情,但它毕竟是给浏览器认的东西,对开发者来说,Css缺乏很多特性,例如变量.常量以及一些编程语法,代码难易组织和维护.这时Css预处理器就应运而生了.Cs ...
- Ambient Occlusion
一般在光照模型中,ambient light的计算方法为:A = l * m,其中l表示表面接收到的来自光源的ambient light的总量,而m表示表面接收到ambient light后,反射和吸 ...
- hdu 1800 Flying to the Mars(简单模拟,string,字符串)
题目 又来了string的基本用法 //less than 30 digits //等级长度甚至是超过了int64,所以要用字符串来模拟,然后注意去掉前导零 //最多重复的个数就是答案 //关于str ...
- iOS正则匹配手机号
#pragma 正则匹配手机号 + (BOOL)validateMobile:(NSString *)mobileNum { /** * 手机号码 * 移动:134[0-8 ...
- Codeforces Round #258 (Div. 2)(A,B,C,D)
题目链接 A. Game With Sticks time limit per test:1 secondmemory limit per test:256 megabytesinput:standa ...
- (3)VS2010+Opencv-2.4.8的配置攻略
这是windows平台上的东西,我为什么要写到安卓这一块呢 因为作者做的安卓方面的东西需要先在windows平台实现一下,所以就想写这篇东西,也参考了网上很多教程,不得不感叹,这些软件版本更新的太快. ...
- Linux网络编程5——使用UDP协议实现群聊
引言 本文实现的功能类似于我之前所写的一篇博文(Linux之select系统调用_2),区别在于进程之间的通信方式有所不同.之前的文章中,我所使用的是管道,而本文我将会使用socket接口. 需求 客 ...