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
相关推荐
lch2011_yb1 分钟前
CSP-J 2023 小苹果
算法
星星.72212 分钟前
【图论】最小生成树|Prim+Kruskal算法
数据结构·c++·算法·图论
圣保罗的大教堂14 分钟前
leetcode 3734. 大于目标字符串的最小字典序回文排列 困难
leetcode
_Narcissus_26 分钟前
单调栈笔记及例题详解
数据结构·c++·笔记·算法·力扣·单调栈·洛谷
土司大王8 小时前
LeetCode hot100——对称二叉树
算法·leetcode·职场和发展
sali-tec9 小时前
C# 基于OpenCv的视觉工作流-章106-差值追踪
图像处理·人工智能·opencv·算法·计算机视觉
Navigator_Z9 小时前
LeetCode //C - 1208. Get Equal Substrings Within Budget
c语言·算法·leetcode
手写码匠9 小时前
Dify 多 Agent 工具权限与安全沙箱实战:让智能体“有能力,但不越权“
人工智能·深度学习·算法·aigc
黎阳之光9 小时前
打破堆场感知黑盒:黎阳之光视频孪生,构建港口码头网格化透明管控新体系
大数据·人工智能·算法·安全·数字孪生