Leetcode刷题——链表就地反转

5.链表就地反转

链表的就地反转模式在不使用额外空间的情况下反转链表的部分内容

在需要反转链表的部分时使用此模式。

LeetCode题目:

反转链表(LeetCode#206)

题目描述

给你单链表的头节点 head ,请你反转链表,并返回反转后的链表。

示例 1:

输入:head = [1,2,3,4,5]

输出:[5,4,3,2,1]

示例 2:

输入:head = [1,2]

输出:[2,1]

示例 3:

输入:head = []

输出:[]

提示:

链表中节点的数目范围是 [0, 5000]

-5000 <= Node.val <= 5000

进阶:链表可以选用迭代或递归方式完成反转。你能否用两种方法解决这道题?

题目求解

python 复制代码
## 迭代的方式
# Definition for singly-linked list.
# class ListNode:
#     def __init__(self, val=0, next=None):
#         self.val = val
#         self.next = next
class Solution:
    def reverseList(self, head: Optional[ListNode]) -> Optional[ListNode]:
        pre, cur = None, head
        while cur:
            item = cur.next
            cur.next = pre
            pre = cur
            cur = item
        return pre
# 简化语句
class Solution:
    def reverseList(self, head: Optional[ListNode]) -> Optional[ListNode]:
        pre, cur = None, head
        while cur:
            cur.next, pre, cur = pre, cur, cur.next
        return pre
# 递归
class Solution:
    def reverseList(self, head: Optional[ListNode]) -> Optional[ListNode]:
        # 递归
        # 当前节点为n_k,则希望 n_{k+1}指向n_k
        # 递归终止条件:空节点 或 只有一个节点(直接返回自身)
        if not head or not head.next:
            return head

        newHead = self.reverseList(head.next)
        
        # 核心逻辑:让当前节点的下一个节点指向自己
        head.next.next = head
        # 切断原指向,避免循环
        head.next = None
        
        # 返回反转后的新头节点
        return newHead

反转链表Il(LeetCode#92)

题目描述

给你单链表的头指针 head 和两个整数 left 和 right ,其中 left <= right 。请你反转从位置 left 到位置 right 的链表节点,返回 反转后的链表 。

示例 1:

输入:head = [1,2,3,4,5], left = 2, right = 4

输出:[1,4,3,2,5]

示例 2:

输入:head = [5], left = 1, right = 1

输出:[5]

提示:

链表中节点数目为 n

1 <= n <= 500

-500 <= Node.val <= 500

1 <= left <= right <= n

进阶: 你可以使用一趟扫描完成反转吗?

题目求解

python 复制代码
# Definition for singly-linked list.
# class ListNode:
#     def __init__(self, val=0, next=None):
#         self.val = val
#         self.next = next
class Solution:
    def reverseBetween(self, head: Optional[ListNode], left: int, right: int) -> Optional[ListNode]:
        item = dummy = ListNode(-1)
        item.next = dummy.next = head
        for _ in range(left - 1):
            item = item.next

        pre = None
        cur = item.next
        for _ in range(right - left + 1):
            cur.next, pre, cur = pre, cur, cur.next
        item.next.next = cur
        item.next = pre
        
        return dummy.next

成对交换节点(LeetCode#24)

题目描述

给你一个链表,两两交换其中相邻的节点,并返回交换后链表的头节点。你必须在不修改节点内部的值的情况下完成本题(即,只能进行节点交换)。

示例 1:

输入:head = [1,2,3,4]

输出:[2,1,4,3]

示例 2:

输入:head = []

输出:[]

示例 3:

输入:head = [1]

输出:[1]

提示:

链表中节点的数目在范围 [0, 100] 内

0 <= Node.val <= 100

题目求解

python 复制代码
# Definition for singly-linked list.
# class ListNode:
#     def __init__(self, val=0, next=None):
#         self.val = val
#         self.next = next
class Solution:
    def swapPairs(self, head: Optional[ListNode]) -> Optional[ListNode]:
        if not head or not head.next:
            return head
        
        item = ListNode(-1)
        item.next = head
        cur = item
        while cur.next and cur.next.next:
            node1, node2 = cur.next, cur.next.next

            cur.next, node1.next, node2.next = node2, node2.next, node1

            cur = node1
        
        return item.next
相关推荐
仟濹2 小时前
【算法打卡day22(2026-03-14 周六)今日算法or技巧:双指针 & 链表】9个题
数据结构·算法·链表·双指针
RechoYit2 小时前
数学建模——评价与决策类模型
python·算法·数学建模·数据分析
地平线开发者2 小时前
地平线 Sparse 多任务参考算法 SparseBevFusionMultitaskOE-V1.0
算法·自动驾驶
OKkankan2 小时前
红黑树的原理及实现
开发语言·数据结构·c++·算法
Eward-an2 小时前
高效构建长度为 n 的开心字符串中第 k 小的字符串
python·leetcode
Jasmine_llq2 小时前
《B3953 [GESP202403 一级] 找因数》
算法·因数枚举算法(核心逻辑)·顺序遍历算法·单输入处理·逐行输出处理·整数算术运算
Eward-an3 小时前
【详细解析】删除有序数组中的重复项 II
数据结构·算法
sg_knight3 小时前
OpenClaw 能做什么?几个真实使用场景说明
算法·ai·大模型·llm·agent·openclaw·小龙虾
嫂子开门我是_我哥3 小时前
心电域泛化研究从0入门系列 | 第七篇:全流程闭环与落地总结——系列终篇
人工智能·算法·机器学习