题目如下:

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 1:

Input: 1->2->3->4->5->NULL
Output: 1->3->5->2->4->NULL

Example 2:

Input: 2->1->3->5->6->4->7->NULL
Output: 2->3->6->7->1->5->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 ...

解题思路:本题难度不大,分别维护奇/偶节点的两个头指针,然后遍历链表,分别将指针指向对方的next,指向后再把自己移动到之前指向的next即可。

代码如下:

# 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
"""
if head == None or head.next == None:
return head
evenHead = even = head.next
odd = head
while odd != None and even != None:
odd.next = even.next
if odd.next != None:
odd = odd.next
even.next = odd.next
even = even.next
odd.next = evenHead
return head

【leetcode】328. Odd Even Linked List的更多相关文章

  1. 【LeetCode】328. Odd Even Linked List 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...

  2. 【Leetcode】 328. Odd Even Linked List

    Given a singly linked list, group all odd nodes together followed by the even nodes. Please note her ...

  3. 【一天一道LeetCode】#328 Odd Even Linked List

    一天一道LeetCode系列 (一)题目 Given a singly linked list, group all odd nodes together followed by the even n ...

  4. 【LeetCode】975. Odd Even Jump 解题报告(C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 动态规划 日期 题目地址:https://leetc ...

  5. 【leetcode】Intersection of Two Linked Lists

    题目简述: Write a program to find the node at which the intersection of two singly linked lists begins. ...

  6. 【leetcode】Intersection of Two Linked Lists(easy)

    Write a program to find the node at which the intersection of two singly linked lists begins. For ex ...

  7. 【LeetCode】Intersection of Two Linked Lists(相交链表)

    这道题是LeetCode里的第160道题. 题目讲的: 编写一个程序,找到两个单链表相交的起始节点. 如下面的两个链表: 在节点 c1 开始相交. 示例 1: 输入:intersectVal = 8, ...

  8. 【LeetCode】链表 linked list(共34题)

    [2]Add Two Numbers (2018年11月30日,第一次review,ko) 两个链表,代表两个整数的逆序,返回一个链表,代表两个整数相加和的逆序. Example: Input: ( ...

  9. <LeetCode OJ> 328. Odd Even Linked List

    328. Odd Even Linked List Total Accepted: 9271 Total Submissions: 24497 Difficulty: Easy Given a sin ...

随机推荐

  1. java中形参中的 “. . .” 是什么意思

    如这个jdbc中封装的绑定参数的方法: /** * 绑定参数 * @param pstmt * @param os */ public static void executebindParam(Pre ...

  2. 【leetcode】998. Maximum Binary Tree II

    题目如下: We are given the root node of a maximum tree: a tree where every node has a value greater than ...

  3. ceph cache pool配置

    https://my.oschina.net/hanhanztj/blog/515410 0.引入 本文介绍如何配置cache pool tiering. cache pool的作用是提供可扩展的ca ...

  4. centos下mysql密码修改与远程连接

    之前的服务器,好久没上去过了,今天上去,密码居然又忘了... 要修改mysql的登陆密码,首先要设置 #vim /etc/my.cnf 在[mysqld] 下面加上一句 skip-grant-tabl ...

  5. vue2 的 过渡(动画)效果

    1.在过渡 效果的使用中 ,key属性需要注意 : 有相同父元素的子元素必须有独特的 key.重复的 key 会造成渲染错误.       参考官方说明:  https://cn.vuejs.org/ ...

  6. PostgreSQL的约束

    约束类型:检查约束.非空约束.唯一约束.主键.外键 1.  检查约束 设置某个字段里的数值必须满足约束表达式的条件. 例:限制人的年龄在0~120之间,语句如下: create table perso ...

  7. Hive 数据类型转换(转)

    原文连接:https://www.iteblog.com/archives/892.html 在<Hive内置数据类型>文章中,我们提到了Hive内置数据类型由基本数据类型和复杂数据类型组 ...

  8. 机器学习笔记--classification_report&精确度/召回率/F1值

    https://blog.csdn.net/akadiao/article/details/78788864 准确率=正确数/预测正确数=P 召回率=正确数/真实正确数=R F1 F1值是精确度和召回 ...

  9. openlayers中单击获取要素

    openlayers中单击获取要素 分类专栏: GIS 总结 OpenLayers   版权声明:本文为博主原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明. 本文链接: ...

  10. Java核心基础知识(一):概念、语法、使用、源码

    1. Java中OOP的特点? OOP(Object Oriented Programming):面向对象编程.具有封装.继承.多态三大特征. 封装:解决数据安全性问题: 继承:解决代码的重用性问题: ...