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 ...
随机推荐
- [bzoj 3687]简单题 bitset的运用
题意 给定一个正整数集,求所有子集算术和的异或和 题解 每次加入一个元素x,用原集合a xor (a<< x) 然后每一个值统计一下 bitset看起来很优越,是一个能位运算的布尔数组 ...
- c++ 继承和组合的区别
.什么是继承 A继承B,说明A是B的一种,并且B的所有行为对A都有意义 eg:A=WOMAN B=HUMAN A=鸵鸟 B=鸟 (不行),因为鸟会飞,但是鸵鸟不会. .什么是组合 若在逻辑上A是B的“ ...
- highchart 导出图片, 显示空白
使用highchart时, 导出的图片会变空白.. 解决方案: 不要加载grid.js
- C编译错误解决方法
1.expected identifier before numeric constant 一般情况下是枚举类型中的某个变量已经被#define定义过一次了,在项目空间中搜索你枚举类型中的所有变量类型 ...
- 如何优化C语言代码(程序员必读)
1.选择合适的算法和数据结构 应该熟悉算法语言,知道各种算法的优缺点,具体资料请参见相应的参考资料,有很多计算机书籍上都有介绍.将比较慢的顺序查找法用较快的二分查找或乱序查找法代替,插入排序或冒泡排序 ...
- Java递归列出目录下全部文件
Java递归列出目录下全部文件 /** * 列出指定目录的全部内容 * */ import java.io.*; class hello{ public static void main(String ...
- Chapter 4 持久存储数据对象
1.使用with open("filename.扩展名","r/w/rb/wb") as data代替data=open(..);data.close() 打开 ...
- 再深入一点ajax
1.建立兼容性强的XHR对象有那么复杂么? 看过一些书,书上为了写针对低版本IE和其他非IE浏览器需要写一大串兼容函数,典型的就是JS高级程序上的. 可是在现实开发中,为了兼容IE6/IE7,只需要这 ...
- tomcat 常见错误
1.启动时加载会话错误 Exception loading sessions from persistent storage 处理方法:删除tomcat对应的项目目录下 work/session.se ...
- PHP一些函数
函数不定参数: func_num_args // 获得参数个数 func_get_args // 获得参数数组 call_user_func('function_name', $parameter) ...