mycode

# 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
"""
Odd = head
Even = dummyeven = head.next
while Even and Even.next:
print(Even.val,Odd.val,Odd.next.val,Odd.next.next.val)
Even.next = Even.next.next #3 6 7
print(Even.next.val,Odd.next.val,Odd.next.next.val)
Even = Even.next
Odd.next = Odd.next.next # 5 4 Null
print(Odd.next.val,Odd.next.val,Odd.next.next.val)
Odd = Odd.next
print(Even.val,Odd.val)
Odd.next = dummyeven #因为无论even=None还是Even.next=None,其实在没有Even = Even.next之前,Even.next=head链中它的下一个,所以不用分类讨论加不加None return head

错误原因:Even节点变化之后,Odd取next和.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 not head:
return head
Odd = head
Even = dummyeven = head.next
while Even and Even.next:
Odd.next = Even.next
Odd = Odd.next
Even.next = Odd.next #3 6 7
Even = Even.next
Odd.next = dummyeven #因为无论even=None还是Even.next=None,其实在没有Even = Even.next之前,Even.next=head链中它的下一个,所以不用分类讨论加不加None
return head

leetcode-mid-Linked list-328 Odd Even Linked List-NO的更多相关文章

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

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

  2. [LeetCode] 328. Odd Even Linked List ☆☆☆(奇偶节点分别放一起)

    每天一算:Odd Even Linked List 描述 给定一个单链表,把所有的奇数节点和偶数节点分别排在一起.请注意,这里的奇数节点和偶数节点指的是节点编号的奇偶性,而不是节点的值的奇偶性. 请尝 ...

  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】328. Odd Even Linked List 解题报告(Python & C++)

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

  5. LeetCode 328. Odd Even Linked List

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

  6. Java [Leetcode 328]Odd Even Linked List

    题目描述: Given a singly linked list, group all odd nodes together followed by the even nodes. Please no ...

  7. 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 ...

  8. (链表) leetcode 328. Odd Even Linked List

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

  9. 【Leetcode】 328. Odd Even Linked List

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

  10. 【leetcode】328. Odd Even Linked List

    题目如下: Given a singly linked list, group all odd nodes together followed by the even nodes. Please no ...

随机推荐

  1. 画一个心送给心爱的小姐姐,Python绘图库Turtle

    Python绘图库Turtle Turtle介绍 Turtle是Python内嵌的绘制线.圆以及其他形状(包括文本)的图形模块. 一个Turtle实际上是一个对象,在导入Turtle模块时,就创建了对 ...

  2. Java 接口和多态练习

    我们鼠标和键盘实现USB接口,那么我们鼠标和键盘就变成了USB设备,这时候我们就可以把它放到笔记本电脑里面去用 package com.biggw.day10.demo07; /** * @autho ...

  3. 并发编程时守护进程在pycharm与python shell中的运行结果不同

    原代码如下 from multiprocessing import Process import time import random def task(name): print('%s is run ...

  4. 如何使用js在移动端和PC端居中

    在手机移动端和PC端控制居中是一个很蛋痛的问题,因为屏幕宽度在变化,所以就不要写死样式,那么我想用JS来控制,灵活的控制宽度,需要注意这三个时候: (1)首先需要在页面刚加载的时候就调用此函数, (2 ...

  5. Red Hat Enterprise Linux查看系统版本命令

    # arch 返回结果为i686表示32位系统,x86_64表示64位系统. # uname -a # uname -r # lsb_release -a # cat /proc/version # ...

  6. 2014-04-27 南江滨大道 6KM 晴

    33分41秒,6.03公里,慢速跑,中间有停了几次拍照 天气不错,多云 人,不多 不知道这货叫啥 2个大人3个小孩,跳绳,小时候的回忆,啊哈 老中少三代,捡风筝也是一种幸福 一家三口,江滨散步,惬意至 ...

  7. Foundation框架下的常用类(NSNumber, NSValue, NSDate,NSDateFormatter)

    1.NSNumber 将基础数类型数据转成对象数据(比如int  float double BOOL  long等等) //通过NSNumber将基础数类型数据转成对象数据. NSNumber * i ...

  8. nodejs 操作 mysql

    1.安装插件 npm install mysql 2.调用代码 var mysql = require('mysql') var connection = mysql.createConnection ...

  9. STM32 总线

    AHB,是Advanced High performance Bus的缩写,译作高级高性能总线,这是一种“系统总线”.AHB主要用于高性能模块(如CPU.DMA和DSP等)之间的连接.AHB 系统由主 ...

  10. Red Hat Enterprise Linux 8.0 安装

    Red Hat Enterprise Linux 8.0 安装 本次安装通过使用VMware Workstation 15 pro 进行. 1.新建虚拟机 2.点击首页的创建新的虚拟机,或者点击标签栏 ...