跳到主要内容
极客日志极客日志面向AI+效率的开发者社区
首页博客我的书AI学习GitHub 精选镜像AI 生图工具UI配色美学关于
搜索内容 / 工具 / 仓库 / 镜像...⌘K搜索
注册
博客列表
Python算法

力扣 Hot100 链表专题:反转与排序链表 (Python 版)

力扣 Hot100 链表中涉及反转与排序的经典题目,包括单链表反转、指定区间反转、K 个一组翻转链表、合并两个有序链表及排序链表。通过快慢指针、递归分治及迭代哨兵节点等方法,提供了详细的 Python 代码实现与思路解析,帮助掌握链表核心操作。

墨染流年发布于 2026/3/30更新于 2026/9/1163 浏览
力扣 Hot100 链表专题:反转与排序链表 (Python 版)

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

1.1、206. 反转链表

算法图解

Python 代码实现
class ListNode:
    def __init__(self, val=0, next=None):
        self.val = val
        self.next = next

class Solution:
    def reverseList(self, head: ListNode) -> ListNode:
        pre = None
        cur = head
        while cur:
            nxt = cur.next
            cur.next = pre
            pre = cur
            cur = nxt
        return pre

1.2、92. 反转链表 II

算法图解

思路

整体来看,p0.next 是反转前的第 left 个节点,pre 是反转后的头节点,cur 是当前遍历到的节点。

Python 代码实现
class Solution:
    def reverseBetween(self, head: ListNode, left: int, right: ) -> ListNode:
        p0 = dummy = ListNode(=head)
        
         _  (left - ):
            p0 = p0.
        
        pre = 
        cur = p0.
        
         _  (right - left + ):
            nxt = cur.
            cur. = pre
            pre = cur
            cur = nxt
        
        p0.. = cur
        p0. = pre
         dummy.
int
next
# 找到 left 的前一个节点
for
in
range
1
next
None
next
# 反转区间 [left, right]
for
in
range
1
next
next
next
next
next
return
next

算法图解

K 个一组翻转链表代码
class Solution:
    def reverseKGroup(self, head: ListNode, k: int) -> ListNode:
        n = 0
        cur = head
        while cur:
            n += 1
            cur = cur.next
        
        p0 = dummy = ListNode(next=head)
        pre = None
        cur = head
        
        while n >= k:
            n -= k
            for _ in range(k):
                nxt = cur.next
                cur.next = pre
                pre = cur
                cur = nxt
            
            nxt = p0.next
            nxt.next = cur
            p0.next = pre
            p0 = nxt
        
        return dummy.next

二、148. 排序链表

2.1、876. 链表的中间结点

算法图解

Python 代码实现(快慢指针)
from typing import Optional

class Solution:
    def middleNode(self, head: Optional[ListNode]) -> Optional[ListNode]:
        t1 = t2 = head
        while t2 and t2.next:
            t1 = t1.next
            t2 = t2.next.next
        return t1

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

算法图解

Python 代码实现
class Solution:
    def mergeTwoLists(self, list1: Optional[ListNode], list2: Optional[ListNode]) -> Optional[ListNode]:
        dummy = ListNode()
        cur = dummy
        while list1 and list2:
            if list1.val <= list2.val:
                cur.next = list1
                cur = cur.next
                list1 = list1.next
            else:
                cur.next = list2
                cur = cur.next
                list2 = list2.next
        if list1:
            cur.next = list1
        else:
            cur.next = list2
        return dummy.next

算法图解

思路:归并排序

找到链表的中间节点,断开为前后两端,分别排序前后两端,排序后再合并两个有序链表。

Python 代码实现
class Solution:
    def middleNode(self, head: Optional[ListNode]) -> Optional[ListNode]:
        slow = fast = head
        while fast and fast.next:
            pre = slow
            slow = slow.next
            fast = fast.next.next
        pre.next = None
        return slow

    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.next
            else:
                cur.next = list2
                list2 = list2.next
            cur = cur.next
        cur.next = list1 if list1 else list2
        return dummy.next

    def sortList(self, head: Optional[ListNode]) -> Optional[ListNode]:
        if not head or not head.next:
            return head
        
        head2 = self.middleNode(head)
        head = self.sortList(head)
        head2 = self.sortList(head2)
        return self.mergeTwoLists(head, head2)

目录

  1. 一、25. K 个一组翻转链表
  2. 1.1、206. 反转链表
  3. Python 代码实现
  4. 1.2、92. 反转链表 II
  5. 思路
  6. Python 代码实现
  7. K 个一组翻转链表代码
  8. 二、148. 排序链表
  9. 2.1、876. 链表的中间结点
  10. Python 代码实现(快慢指针)
  11. 2.2、21. 合并两个有序链表
  12. Python 代码实现
  13. 思路:归并排序
  14. Python 代码实现

更多推荐文章

查看全部
  • 本地部署 Stable Diffusion 3.5 完整教程
  • Python 接入天远劳动仲裁 API 构建企业风控体系
  • 算法分配测试任务:管理权威的数字化消解
  • Wave Terminal 跨平台终端工具安装与使用指南
  • LLaMA-Factory 数据集制作与 Qwen3 模型微调评估流程
  • 前端框架选型指南:React、Vue 与 Angular 对比分析
  • Ollama 实战:使用 Spring AI 调用本地大模型
  • 基于 GLM-4.6 与 Trae 的 AI 面试教练系统实战
  • 主流无人机厂商 RemoteID 支持情况汇总
  • MacOS 命令行工具详解与基础操作指南
  • 利用提示词消除 AI 写作痕迹的实战技巧
  • LINUX DO 社区无需邀请码注册流程说明
  • 大模型微调(Fine-tuning)原理与实战指南
  • C++ 观察者设计模式核心原理与代码实现
  • 大模型行业三大核心竞争力:资金、人才与数据
  • 深度学习模型优化策略与实战调参
  • 实战 LLaMA Factory:在国产 DCU 上高效微调 Llama 3 模型
  • OpenClaw 跨平台部署:WSL Ubuntu 与 CentOS9 安装及飞书对接
  • OpenAI 集成 Langchain 操作实战详解
  • Ubuntu 20.04 缺少 WiFi 图标的驱动排查与修复方案

相关免费在线工具

  • 加密/解密文本

    使用加密算法(如AES、TripleDES、Rabbit或RC4)加密和解密文本明文。 在线工具,加密/解密文本在线工具,online

  • Gemini 图片去水印

    基于开源反向 Alpha 混合算法去除 Gemini/Nano Banana 图片水印,支持批量处理与下载。 在线工具,Gemini 图片去水印在线工具,online

  • curl 转代码

    解析常见 curl 参数并生成 fetch、axios、PHP curl 或 Python requests 示例代码。 在线工具,curl 转代码在线工具,online

  • Base64 字符串编码/解码

    将字符串编码和解码为其 Base64 格式表示形式即可。 在线工具,Base64 字符串编码/解码在线工具,online

  • Base64 文件转换器

    将字符串、文件或图像转换为其 Base64 表示形式。 在线工具,Base64 文件转换器在线工具,online

  • Markdown转HTML

    将 Markdown(GFM)转为 HTML 片段,浏览器内 marked 解析;与 HTML转Markdown 互为补充。 在线工具,Markdown转HTML在线工具,online