【leetcode】328. Odd Even Linked List
来源:互联网 发布:网络诈骗防范常识电影 编辑:程序博客网 时间:2024/06/03 22:36
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.
var oddEvenList = function(head) { if(head === null||head.next === null){ return head; } var flag = true; var h1 = head; var h2 = head.next; var s2 = h2; var temp = h2.next; while(temp!==null){ if(flag){ h1.next = temp; h1 = h1.next; }else{ h2.next = temp; h2=h2.next; } flag = !flag; temp = temp.next; } h2.next=null h1.next = s2; return head;};
对于自己,要注意的点也是老问题,也就是处理[] 和[1]这种空指针的时候,开头很重要,还有一点中间出错的是 对于最后的节点,要额外赋值 null ,特别是偶数列的尾巴。,不然会出错不能够转化。另外flag要记得转。
0 0
- 328. Odd Even Linked List leetcode(list)
- LeetCode—328. Odd Even Linked List
- LeetCode 328. Odd Even Linked List Java
- Leetcode 328. Odd Even Linked List
- <LeetCode><Easy> 328. Odd Even Linked List
- [leetcode] - 328. Odd Even Linked List
- <LeetCode OJ> 328. Odd Even Linked List
- 328. Odd Even Linked List LeetCode
- leetCode 328. Odd Even Linked List
- Leetcode: 328. Odd Even Linked List(JAVA)
- LeetCode--328. Odd Even Linked List
- LeetCode 328. Odd Even Linked List
- LeetCode 328. Odd Even Linked List
- [LeetCode]328. Odd Even Linked List
- 【LeetCode】328. Odd Even Linked List
- LeetCode 328. Odd Even Linked List
- leetcode 328. Odd Even Linked List
- [leetcode]328. Odd Even Linked List
- THUSC 2016 行记
- 修改UITextView或者UITextField的光标
- 用JavaScript玩转计算机图形学(一)光线追踪入门
- C#实现压缩HTML代码的方法
- Eclipse 安装需要的 JDK 版本简要说明
- 【leetcode】328. Odd Even Linked List
- python 安装NumPy
- C#自写的一个HTML解析类(类似XElement语法)
- 用JavaScript玩转计算机图形学(二)基本光源
- 使用STL去除std::vector自定义结构体重复项
- 举例配置hadoop完全分布式集群(准备节点数4个,h15、h16、h17、h18)
- 基于TCP/IP的网络编程
- C# Stream 和 byte[] 之间的转换
- C# vs C++ 全局照明渲染性能比试