当前位置: 首页 > news >正文

力扣hot100_链表(3)_python版本

一、25. K 个一组翻转链表

1.1、206. 反转链表

在这里插入图片描述

  • py代码
class ListNode:def __init__(self, val=0, next= node):self.val = valself.next = next
class Solution:def reverseList(self, head):pre = Nonecur = headwhile cur:next = cur.nextcur.next = prepre = curcur = nextreturn pre

1.2、92. 反转链表 II

在这里插入图片描述

  • 思路:
    整体来看,1是反转前的第left个节点(p0.next),pre(4)是反转的后的头节点,cur是当前遍历到的节点下一个(5)。
class Solution:def reverseBetween(self, head, left, right):p0 = dummy = ListNode(next = head) # 这样一起实例化,p0和dummy永远都是指向同一位置for _ in range(left-1):p0 = p0.next  # 知道转换的前一个结点pre = Nonecur = p0.next     # 当前正在处理的节点for _ in range(right-left+1)nxt = cur.nextcur.nxt = prepre = curcur = nxtp0.next.next = curp0.next = prereturn dummy.next

在这里插入图片描述

  • 代码:
class Solution:def reverseKGroup(self, head, k):n = 0cur = headwhile cur:n += 1cur = cur.nextp0 = dummy = ListNode(next = head)pre = Nonecur = head# k个一组处理while n >= k:n -= kfor _ in range(k):nxt = cur.nextcur.next = prepre = curcur = nxtnxt = p0.nextnxt.next = curp0.next = prep0 = nxt

二、148. 排序链表

2.1、876. 链表的中间结点

在这里插入图片描述

  • 代码:这道题典型的快慢指针
# Definition for singly-linked list.
# class ListNode:
#     def __init__(self, val=0, next=None):
#         self.val = val
#         self.next = next
class Solution:def middleNode(self, head: Optional[ListNode]) -> Optional[ListNode]:t1 = t2 = headwhile t2 and t2.next:t1 = t1.nextt2 = t2.next.nextreturn t1

2.2、21. 合并两个有序链表

在这里插入图片描述

  • 代码:
class Solution:def mergeTwoLists(self, list1: Optional[ListNode], list2: Optional[ListNode]) -> Optional[ListNode]:dummy = ListNode()cur = dummywhile list1 and list2:if list1.val <= list2.val:cur.next = list1cur = cur.nextlist1 = list1.nextelse:cur.next = list2cur = cur.nextlist2 = list2.nextif list1:cur.next = list1else:cur.next = list2return dummy.next

在这里插入图片描述

  • 思路1:归并排序
    找到链表的中间节点,断开为前后两端,分别排序前后两端,排序后,再合并两个有序链表。
  • 代码1:
class Solution:# 876. 链表的中间结点(快慢指针)def middleNode(self, head: Optional[ListNode]) -> Optional[ListNode]:slow = fast = headwhile fast and fast.next:pre = slow  # 记录 slow 的前一个节点slow = slow.nextfast = fast.next.nextpre.next = None  # 断开 slow 的前一个节点和 slow 的连接return slow# 21. 合并两个有序链表(双指针)def mergeTwoLists(self, list1: Optional[ListNode], list2: Optional[ListNode]) -> Optional[ListNode]:cur = dummy = ListNode()  # 用哨兵节点简化代码逻辑while list1 and list2:if list1.val < list2.val:cur.next = list1  # 把 list1 加到新链表中list1 = list1.nextelse:  # 注:相等的情况加哪个节点都是可以的cur.next = list2  # 把 list2 加到新链表中list2 = list2.nextcur = cur.nextcur.next = list1 if list1 else list2  # 拼接剩余链表return dummy.nextdef sortList(self, head: Optional[ListNode]) -> Optional[ListNode]:# 如果链表为空或者只有一个节点,无需排序if head is None or head.next is None:return head# 找到中间节点 head2,并断开 head2 与其前一个节点的连接# 比如 head=[4,2,1,3],那么 middleNode 调用结束后 head=[4,2] head2=[1,3]head2 = self.middleNode(head)# 分治head = self.sortList(head)head2 = self.sortList(head2)# 合并return self.mergeTwoLists(head, head2)

相关文章:

  • 盈达科技:登顶GEO优化全球制高点,以AICC定义AI时代内容智能优化新标杆
  • TCP四大特性面试回答引导
  • 【无人机】无人机位置估计出现偏差的原因分析
  • ESP32-S3开发板麦克风录音到SD卡存储测试
  • 自主可控鸿道Intewell工业实时操作系统
  • Rust 语言使用场景分析
  • 【LangChain4j】AI 第一弹:LangChain4j 的理解
  • 图聚类中的亲和力传播
  • 数据库11(触发器)
  • 跨平台软件开发探讨
  • 三目云台转动性能稳定性
  • 动态显微镜数据分析及AI拓展
  • 第50讲:AI+农业金融与风险预测场景实战
  • Centos9安装docker
  • spark和hadoop之间的对比关系和联系
  • 《MySQL:MySQL表的内外连接》
  • 线程入门3
  • es 自动补全
  • Available platform plugins are: xcb.报错解决办法
  • 2026《数据结构》考研复习笔记六(串的KMP算法)
  • 最高法强化涉新就业形态民事案件审判指导:推动出台司法解释
  • 这座“蚌埠住了”的城市不仅会接流量,也在努力成为文旅实力派
  • 言短意长|新能源领军者密集捐赠母校
  • 新华每日电讯:从上海街区经济看账面、市面、人面、基本面
  • “上海-日喀则”直飞航线正式通航,将于5月1日开启首航
  • 第二艘国产大型邮轮爱达·花城号完成坞内起浮