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
.
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
public class Solution {
public ListNode oddEvenList(ListNode head) {
ListNode oddHead = new ListNode();
ListNode oddCurrent = oddHead; ListNode evenHead = new ListNode();
ListNode evenCurrent = evenHead; int flag = ;
while(head != null) {
if (flag == ) {
oddCurrent.next = head;
oddCurrent = oddCurrent.next;
} else {
evenCurrent.next = head;
evenCurrent = evenCurrent.next;
}
head = head.next;
flag *= -;
}
evenCurrent.next = null;
oddCurrent.next = evenHead.next;
return oddHead.next;
}
}
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)
328. 奇偶链表 328. Odd Even Linked List 题目描述 给定一个单链表,把所有的奇数节点和偶数节点分别排在一起.请注意,这里的奇数节点和偶数节点指的是节点编号的奇偶性,而不是 ...
- [LeetCode] 328. Odd Even Linked List ☆☆☆(奇偶节点分别放一起)
每天一算:Odd Even Linked List 描述 给定一个单链表,把所有的奇数节点和偶数节点分别排在一起.请注意,这里的奇数节点和偶数节点指的是节点编号的奇偶性,而不是节点的值的奇偶性. 请尝 ...
- [LeetCode] 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 ...
- Leetcode 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 ...
- 328. Odd Even Linked List
Given a singly linked list, group all odd nodes together followed by the even nodes. Please note her ...
- leetcode:Odd Even Linked List
Given a singly linked list, group all odd nodes together followed by the even nodes. Please note her ...
随机推荐
- codev 2147 数星星
2147 数星星 http://codevs.cn/problem/2147/ 题目描述 Description 小明是一名天文爱好者,他喜欢晚上看星星.这天,他从淘宝上买下来了一个高级望远镜.他十分 ...
- .net core 一次坑爹的类库打包过程
众所周知,.net core 跨平台类库引用一定要通过nuget获得.(如有问题,欢迎指出) 打包 将普通.net project转换成.net core 的类库有两种方式: 1.新建.net cor ...
- python 小练手
监控 主动监控 - 服务器端轮询客户端 被动监控-客户端agent上报到服务器端 混合模式---两种都支持 需求 1个性化的监控需求 2每个服务的监控间隔不同 3混合模式的监控
- 【BZOJ-3195】奇怪的道路 状压DP (好题!)
3195: [Jxoi2012]奇怪的道路 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 305 Solved: 184[Submit][Statu ...
- bzoj2194: 快速傅立叶之二
#include <iostream> #include <cstdio> #include <cstring> #include <cmath> #i ...
- Android实现透明式状态栏
Android实现透明式状态栏 1. 修改style样式 2. 创建values-v19文件夹 3. 在这个文件夹下创建style.xml 4. 对activity_main.xml进行修改 移 ...
- CentOS 6.7下利用Rsyslog+LogAnalyzer+MySQL部署日志服务器
一.简介 LogAnalyzer 是一款syslog日志和其他网络事件数据的Web前端.它提供了对日志的简单浏览.搜索.基本分析和一些图表报告的功能.数据可以从数据库或一般的syslog文本文件中获取 ...
- 【CityHunter】通过Unity3D来制作游戏中AR部分的内容
嗯,最近再考虑,CityHunter中,玩家攻略藏宝图时,为了增加可玩性,应该增强在AR部分的游戏性.最近特别火的游戏<Pokemon Go>在打开摄像头以后,可以看到小精灵,实际上,如果 ...
- BZOJ3110: [Zjoi2013]K大数查询
喜闻乐见的简单树套树= =第一维按权值建树状数组,第二维按下标建动态开点线段树,修改相当于第二维区间加,查询在树状数组上二分,比一般的线段树还短= =可惜并不能跑过整体二分= =另外bzoj上的数据有 ...
- webmagic 增量爬取
webmagic 是一个很好并且很简单的爬虫框架,其教程网址:http://my.oschina.net/flashsword/blog/180623 webmagic参考了scrapy的模块划分, ...