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
相关推荐
Run_Teenage几秒前
算法:树的重心、树的直径
算法
专注仿真1 分钟前
高代价计算后置:探测、交战、毁伤三条链路的统一设计哲学
算法·探测·毁伤
xie0510_2 分钟前
Any类简要实现
开发语言·c++·算法
在所不辞兄14 分钟前
为什么PINN非常适合求解有限元模型
人工智能·深度学习·神经网络·算法·机器学习
一棵树735125 分钟前
数据结构闲聊
数据结构·算法
Nil20827 分钟前
leetcode 152乘积最大子数组
leetcode
bro_Java66627 分钟前
《二叉树的基础知识、操作方法与代码示例》
java·数据结构·算法·广度优先
PascalXie37 分钟前
地面机器人VSLAM技术调研与实现方案参考:算法、硬件平台与工程落地
算法·机器人
aqiu1111113 小时前
【C++算法打怪专栏】8. 字符串转换整数 (atoi)
开发语言·c++·算法
信奥卷王11 小时前
[GESP202609 六级] 分树规划
算法