我的解法:

class Solution:def reverseBetween(self, head: ListNode, m: int, n: int) -> ListNode:count = 0dummy = ListNode()dummy.next = headp = dummyinv_nodes = []while count <= n:if count == m-1:first_pre = pif m <= count <= n:inv_nodes.append(p)p = p.nextcount += 1for i in range(len(inv_nodes)-1,0,-1):inv_nodes[i].next = inv_nodes[i-1]first_pre.next = inv_nodes[-1]inv_nodes[0].next = preturn dummy.next

定义一个伪头节点,从伪头节点开始遍历一遍链表,遍历过程中计数,遍历到第m-1个节点时,用指针first_pre记录指向节点,将第m到第n个节点储存到列表中。遍历到第n+1个节点时,停止遍历,此时p指向第n+1个节点。将列表中的节点从后往前反向链接,在将first_pre指向列表最后一个节点,即将原链表头部接上翻转后的链表,而列表第一个节点指向指针p即接上剩余原链表。时间复杂度最坏情况为O(n),空间复杂度O(n)。

大佬解法:

class Solution:def reverseBetween(self, head: ListNode, m: int, n: int) -> ListNode:count = 1dummy = ListNode()dummy.next = headpre, cur = dummy, headwhile count <= n:if count == m:fp = preif m+1<=count<=n:third = cur.nextcur.next = prepre = curcur = thirdelse:cur = cur.nextpre = pre.nextcount += 1fp.next.next = curfp.next = prereturn dummy.next

利用三个指针可以实现一次遍历修改链表方向O(n),空间复杂度O(1)。

垃圾小白羊的leetcode刷题记录7相关推荐

  1. 垃圾小白羊的leetcode刷题记录6

    我的解法: class Solution:def spiralOrder(self, matrix: List[List[int]]) -> List[int]:if not matrix:re ...

  2. LeetCode刷题记录15——21. Merge Two Sorted Lists(easy)

    LeetCode刷题记录15--21. Merge Two Sorted Lists(easy) 目录 LeetCode刷题记录15--21. Merge Two Sorted Lists(easy) ...

  3. LeetCode刷题记录14——257. Binary Tree Paths(easy)

    LeetCode刷题记录14--257. Binary Tree Paths(easy) 目录 前言 题目 语言 思路 源码 后记 前言 数据结构感觉理论简单,实践起来很困难. 题目 给定一个二叉树, ...

  4. LeetCode刷题记录13——705. Design HashSet(easy)

    LeetCode刷题记录13--705. Design HashSet(easy) 目录 LeetCode刷题记录13--705. Design HashSet(easy) 前言 题目 语言 思路 源 ...

  5. LeetCode刷题记录12——232. Implement Queue using Stacks(easy)

    LeetCode刷题记录12--232. Implement Queue using Stacks(easy) 目录 LeetCode刷题记录12--232. Implement Queue usin ...

  6. LeetCode刷题记录11——290. Word Pattern(easy)

    LeetCode刷题记录11--290. Word Pattern(easy) 目录 LeetCode刷题记录11--290. Word Pattern(easy) 题目 语言 思路 源码 后记 题目 ...

  7. LeetCode刷题记录10——434. Number of Segments in a String(easy)

    LeetCode刷题记录10--434. Number of Segments in a String(easy) 目录 LeetCode刷题记录9--434. Number of Segments ...

  8. LeetCode刷题记录9——58. Length of Last Word(easy)

    LeetCode刷题记录9--58. Length of Last Word(easy) 目录 LeetCode刷题记录9--58. Length of Last Word(easy) 题目 语言 思 ...

  9. LeetCode刷题记录8——605. Can Place Flowers(easy)

    LeetCode刷题记录8--605. Can Place Flowers(easy) 目录 LeetCode刷题记录8--605. Can Place Flowers(easy) 题目 语言 思路 ...

最新文章

  1. mantelhean.test r语言_请教如何将mantel test报告性的结果转化为表格。
  2. 第十届蓝桥杯大赛软件赛省赛 C/C++ 大学B组
  3. 最小二乘多项式拟合程序matlab,最小二乘法的多项式拟合(matlab实现)
  4. C#后台,执行前台js 脚本
  5. 【bfs】Replication G(P7151)
  6. c++远征之模板篇——友元函数、友元类
  7. oracle imp 报12154错误解决办法
  8. file表单元素怎样设置浏览按钮的样式
  9. 51单片机产生1Hz-5kHz可调占空比方波
  10. bat备份mysql数据库
  11. sps的process插件安装包_什么是Process插件?在中介和调节效应分析中有哪些优势和不足?如何下载与安装?...
  12. Java随机数的创建
  13. xbox手柄测试_【喂你播】苹果官网上架Xbox无线手柄,那么未来会有果牌手柄吗?...
  14. 色彩校正中的Gamma(人眼亮度感应的非线性)
  15. Kotlin读书总结之面向对象
  16. 按钮按下时立体感效果
  17. 利用SSM(springmvc+spring+mybatis)实现多表联合查询
  18. Docker常用容器命令
  19. 移动端网页录音上传,服务端智能语音识别
  20. PAT乙级|C语言|1025 反转链表 (25分)

热门文章

  1. 电脑开不了机系统应该如何恢复正常
  2. Lpl and Energy-saving Lamps 计蒜客多校
  3. 【YOLOv5实战2】基于YOLOv5的交通标志识别系统-自定义数据集
  4. Django之验证码(十七)
  5. node aws 内存溢出_如何使用Node和AWS S3设置简单的图像上传
  6. android 开源收藏
  7. 字节跳动Android内部学习资料泄露,高级面试题+解析
  8. r5 7600x和r7 7700x差距
  9. 《阿尔卑斯》观后感 (r10笔记第44天)
  10. maven 报错[WARNING] The artifact aspectj:aspectjrt:jar:1.5.4 has been relocated to org.aspectj:aspectj