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
相关推荐
Java_2017_csdn5 分钟前
ComplexKeysShardingAlgorithm 小结
java·大数据·算法
海梨花10 分钟前
快手面试高频算法题
java·算法·面试
lqqjuly12 分钟前
超分辨率算法深度解析(Super-Resolution Algorithms)
算法
嵌入式老牛2 小时前
液晶段码(米/日字格)识别—倾斜校正
opencv·算法·仿射变换
luj_17682 小时前
残熵算法:风险缓冲与效率优化的融合
c语言·开发语言·网络·经验分享·算法
oddsand12 小时前
pgvector 三大相似度算法
人工智能·算法·机器学习
运筹vivo@3 小时前
LeetCode 2574. 左右元素和的差值
算法·leetcode·职场和发展·每日一题
计算机安禾3 小时前
【数据库系统原理】第4篇:关系数据结构的形式化定义:域、笛卡尔积与关系模式
数据结构·数据库·算法
手写码匠3 小时前
手写 DeepSeek 推理引擎优化:从 FP16 到 INT4 的量化加速实战
人工智能·深度学习·算法·aigc
GuWenyue3 小时前
LeetCode 76 最小覆盖子串|JS 滑动窗口标准解法
前端·算法·面试