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 ...
随机推荐
- Swift中的willSet与didSet
Swift中的willSet与didSet 周银辉 在Swift语言中用了willSet和didSet这两个特性来监视属性的除初始化之外的属性值变化 无需说太多,看看下面的代码你就能很快明白的 imp ...
- [masmplus]初次使用报external symbol _start 是配置问题
初次使用masmplus 其中在 codesg segment 使用了 start 标记, 并在end处标明了:end start 但是默认的masmplus 会提示 start 为 不认识的 e ...
- 20145233 2016-2017 1 linux题目总结
20145233 2016-2017 1 linux题目总结 第一周考试知识汇总 判断:实验楼环境中所有的默认系统用户名和密码均为 shiyanlou.(x ). 填空:Linux Bash中,Ctr ...
- select,poll,epoll比较
除常用文件i/o外,其他常用io模型:io多路复用(select和poll系统调用)信号驱动I/Olinux专有的epoll编程接口异步io(aio),linux在glibc中提供有基于线程的 pos ...
- Java static 静态代码块执行分析
假设有这样一个类: public class Utils { static { Log.i("static","isLoad!"); } public stat ...
- iOS地图 -- 区域监听的实现和小练习
区域监听用到的方法 [self.mgr startMonitoringForRegion:region]; --> 开启区域监听,没有返回值,在代理方法中得到信息并且处理信息 注:该方法只有用户 ...
- Thinking in java学习笔记之String的不可变性
为了提高效率,可以使用StringBuffer或StringBuilder 1. 在执行速度方面的比较:StringBuilder > StringBuffer 2. StringBuffer与 ...
- a版本冲刺第二天
队名:Aruba 队员: 黄辉昌 李陈辉 林炳锋 鄢继仁 张秀锋 章 鼎 学号 昨天完成的任务 今天做的任务 明天要做的任务 困难点 体会 408 学习测试文档的编写 看了构建之法的第二章和十三 ...
- Samba服务配置简明笔记
内部服务器之间拷贝数据,用root账号访问,没有做更复杂的设置. 1.用YUM安装samba服务器端及客户端: [root@tenjs05 init.d]# yum install samba sam ...
- 3 HTML&JS等前端知识系列之javascript的基础
preface 作为一名运维开发,必须懂得前端知识,比如javascript,dom等等,下面就聊聊javascript. include 数据格式 条件判断,循环流程等. 函数 面向对象 what ...