328. Odd Even Linked List——多利用fake_head
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 ...
# Definition for singly-linked list.
# class ListNode(object):
# def __init__(self, x):
# self.val = x
# self.next = None class Solution(object):
def oddEvenList(self, head):
"""
:type head: ListNode
:rtype: ListNode
"""
node = head
i = 1
odd_fake, even_fake = ListNode(None), ListNode(None)
odd_tail, even_tail = odd_fake, even_fake
while node:
if i & 1:
odd_tail.next = node
odd_tail = node
else:
even_tail.next = node
even_tail = node
node = node.next
i += 1
even_tail.next = None
odd_tail.next = even_fake.next
return odd_fake.next
328. Odd Even Linked List——多利用fake_head的更多相关文章
- <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 ☆☆☆(奇偶节点分别放一起)
每天一算:Odd Even Linked List 描述 给定一个单链表,把所有的奇数节点和偶数节点分别排在一起.请注意,这里的奇数节点和偶数节点指的是节点编号的奇偶性,而不是节点的值的奇偶性. 请尝 ...
- LeetCode 328. 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
Given a singly linked list, group all odd nodes together followed by the even nodes. Please note her ...
- 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 C#
Given a singly linked list, group all odd nodes together followed by the even nodes. Please note her ...
- 【一天一道LeetCode】#328 Odd Even Linked List
一天一道LeetCode系列 (一)题目 Given a singly linked list, group all odd nodes together followed by the even n ...
- (链表) 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
Given a singly linked list, group all odd nodes together followed by the even nodes. Please note her ...
随机推荐
- SQL疑难杂症【2】解决SQL订阅过程中找不到已经创建的订阅
之前有写过一篇博客,主要是图解SQL复制技术:图解SQL 2008数据库复制,当时的测试环境是在我本地同一个服务器上面,所以测试的时候可谓是一帆风顺,最近公司要做一个数据同步的事物,所以再次在不同的服 ...
- CUBRID学习笔记 10 数据库文件的类型和含义
demodb contains the database data; demodb_lgar000, 001 and so forth are log archives used for point ...
- C语言程序设计现代方法_基本类型(第七章)
C语言支持两种不同的数值类型,整数类型,浮点类型. C语言的整数类型有不同的尺寸.int类型通常为32位,但在老的CPU上可能是16位.有些可能是64位. 因此,int型如果在16位CPU上最大值就是 ...
- 读jQuery源码有感3
这次的主题是,具体的库和抽象的思路. 当看到Deferred这个区块时,觉得jQuery代码设计挺复杂,得用许多脑力才能看明白. 可是把这个峰回路转十八回的代码看懂又如何,是为了使用过程中出现bug后 ...
- RTMP命令亲自测试记录
手动和自动录像模块: recorder rec1 { record all manual; record_unique on; record_notify on; record_max_size 51 ...
- js的事件处理程序
js事件处理程序一般有三种: 1.HTML事件处理程序 <body> <input type="button" value="点击" oncl ...
- CentOS下源码安装MySQL
一.创建mysql用户与组,相关目录 useradd mysql -s /sbin/nologin mkdir /usr/local/mysql chown -R mysql.mysql mkdir ...
- 让Fiddler能够检测到localhost的http数据
用 vs.net开发调试网站程序时经常有这样的地址: http://localhost:2033/ 然而在开启 Fiddler 后会发现Fiddler 完全抓不到任何封包. 主要的原因是因为 Fidd ...
- 【服务器防护】VPN的ip变更,导致无法连接服务器,解决方法【阿里云ECS】
在阿里云的管理控制台,云服务器ECS - 对应服务器 - 选“管理” - “连接管理终端” 通过这个入口,可以进入Linux云服务器,修改防火墙限制的IP即可
- uva11429(生成随机数 期望)
// // main.cpp // uva11429 // // Created by New_Life on 16/8/4. // Copyright © 2016年 chenhuan001. Al ...