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 ...
随机推荐
- 导入ApiDemo报错,找不到R文件
1.先检查当前ApiDemo对应的SDK版本是否一致(项目右键-Properties-Android) 2.查看是什么错误.我的就是layout中的progressbar_2.xml中所有组件的id前 ...
- 12 高性能I/O框架库Libevent
这里不讲Libevent库的具体内容了,从宏观上对I/O库整体做个介绍 Linux服务器程序必须处理三类事件:I/O事件,信号和定时事件 统一事件源:统一处理这三类事件既能使代码简单易懂,又能避免一些 ...
- vi/vim使用指北 ---- Learning the vi and Vim Editors 读书 笔记
vi/vim作为liux系统下最强大,最流行的文本编辑器之一.边看<Learning the vi and vim Editor>边学习vim,顺便做写简单的笔记,供以后查询. 没看这本书 ...
- 【面试题043】n个骰子的点数
[面试题043]n个骰子的点数 题目: 把n个骰子扔在地上,所有骰子朝上一面的点数之和为s, 输入n,打印出s的所有可能的值出现的概率. n个骰子的总点数,最小为n,最大为6n,根据排列组 ...
- HDU4836 The Query on the Tree(树状数组&&LCA)
由于智力的问题,百度之星完全lu不动..开场看第一题根据题目给的条件我觉得一定是可以构造出来的,题目给的意思颇有鸽巢原理的感觉,于是觉得开场第一题应该就是智力构造题了,想了半个小时,发现完全想不动,于 ...
- iOS搜索栏
百度云连接:http://pan.baidu.com/s/1pJLzDFX
- Java获取最后插入MySQL记录的自增ID值方法
方法一: String sql = "INSERT INTO users (username,password,email) VALUES (?,?,?);"; PreparedS ...
- poj 3072(最短路)
题目链接:http://poj.org/problem?id=3072 一涉及稍微计算几何方面的东西就要做好久,一开始先用SPFA写的,可能是由于松弛次数过多导致精度损失,郁闷了好久,然后改成Dijk ...
- lintcode:数字组合 II
数字组合 II 给出一组候选数字(C)和目标数字(T),找出C中所有的组合,使组合中数字的和为T.C中每个数字在每个组合中只能使用一次. 注意事项 所有的数字(包括目标数字)均为正整数. 元素组合(a ...
- AngularJS学习笔记2——AngularJS的初始化
本文主要介绍AngularJS的自动初始化以及在必要的适合如何手动初始化. Angular <script> Tag 下面通过一小段代码来介绍推荐的自动初始化过程: <!doctyp ...