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

最新文章

  1. jquery双击事件
  2. java获取客户端ID地址
  3. swift 2.x学习笔记(二)
  4. 复旦大学2014--2015学年第二学期高等代数II期末考试情况分析
  5. 集合框架,ArrayList和Vector的区别,让arrayList线程安全的几种方案
  6. Win8 安装驱动
  7. AWS 之Load Balance篇
  8. 我眼中的go的语法特点
  9. CTE-递归[2]
  10. C:\WINDOWS\system32\config\systemprofile\Desktop引用了一个不可用的位置
  11. 《程序设计语言——实践之路(英文第三版)》【PDF】下载
  12. 【CF715E】Complete the Permutations 第一类斯特林数
  13. ES6 Symbol数据类型和set-map 数据结构
  14. Mad Lids游戏 华氏与摄氏温度转换
  15. Django之MVC和MTV
  16. mongodb 通过mongodump来备份Sharded Cluste分片集群
  17. python垃圾回收三之标记清除
  18. Minix2.0操作系统公用头文件说明
  19. maven+testng+reportng的pom设置
  20. FileZilla提权的过程

热门文章

  1. NSCalendar
  2. jQuery实现的简单文字提示效果模拟title(转)
  3. 2013年5月~2013年11月份(转接关于ns51服务平台项目)相关资料:
  4. html5 drap & drop
  5. JAVA入门 第五周 1多项式
  6. hdu 1005 简单题
  7. Volley框架的流程图分析
  8. 升级MySQL支持utf8mb4字符集详细步骤
  9. D3.js 其他选择元素方法
  10. 转!数据库连接池概念、种类、配置(DBCP\C3P0\JndI与Tomact配置连接池)