博客
关于我
【11月打卡~Leetcode每日一题】328. 奇偶链表(难度:中等)
阅读量:260 次
发布时间:2019-03-01

本文共 937 字,大约阅读时间需要 3 分钟。

328. 奇偶链表

给定一个单链表,把所有的奇数节点和偶数节点分别排在一起。请注意,这里的奇数节点和偶数节点指的是节点编号的奇偶性,而不是节点的值的奇偶性。

请尝试使用原地算法完成。你的算法的空间复杂度应为 O(1),时间复杂度应为 O(nodes),nodes 为节点总数。

链表这种题还是要在纸上写一下模拟一下比较好,不然很容易出错,我这道题提交了4次才过,不管总体来说没有什么难思考的点,双指针遍历即可,要注意,在后续修改节点的时候,前面如果有调用,那么前面调用的链表中的该节点已被修改,需要仔细考虑,不然很容易形成死循环

# Definition for singly-linked list.# class ListNode:#     def __init__(self, val=0, next=None):#         self.val = val#         self.next = nextclass Solution:    def oddEvenList(self, head: ListNode) -> ListNode:        if not head or not head.next:            return head        even_stand = head.next        odd,even = head,head.next # odd 奇        while(even.next and even.next.next):            odd.next =odd.next.next            odd = odd.next            even.next = even.next.next            even = even.next        if even.next:            odd.next = odd.next.next            odd = odd.next        even.next = None        odd.next = even_stand        return head

转载地址:http://jhba.baihongyu.com/

你可能感兴趣的文章
nodejs npm常用命令
查看>>
nodejs npm常用命令
查看>>
Nodejs process.nextTick() 使用详解
查看>>
NodeJS yarn 或 npm如何切换淘宝或国外镜像源
查看>>
nodejs 中间件理解
查看>>
nodejs 创建HTTP服务器详解
查看>>
nodejs 发起 GET 请求示例和 POST 请求示例
查看>>
NodeJS 导入导出模块的方法( 代码演示 )
查看>>
nodejs 开发websocket 笔记
查看>>
nodejs 的 Buffer 详解
查看>>
nodejs 的 path 模块详解
查看>>
NodeJS 的环境变量: 开发环境vs生产环境
查看>>
nodejs 读取xlsx文件内容
查看>>
nodejs 运行CMD命令
查看>>
Nodejs+Express+Mysql实现简单用户管理增删改查
查看>>
nodejs+nginx获取真实ip
查看>>
nodejs-mime类型
查看>>
NodeJs——(11)控制权转移next
查看>>
NodeJS、NPM安装配置步骤(windows版本)
查看>>
NodeJS、NPM安装配置步骤(windows版本)
查看>>