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
相关推荐
Forever Nore41 分钟前
LeetCode 1 两数之和
算法·leetcode·职场和发展
To_OC1 小时前
LC 3 无重复字符的最长子串:从入门滑动窗口到优化写法,再也不怕面试官追问
javascript·算法·leetcode
(Charon)1 小时前
【C++】多线程死锁:产生原因、复现与解决方法
开发语言·c++·算法
lucas_AI2 小时前
ConfBench:大模型的「我很有把握」,到底能不能信?
人工智能·算法
oier_Asad.Chen2 小时前
【洛谷题解/AcWing题解/真题】洛谷P2330【SCOI2005】繁忙的都市(Kruskal算法的再探究))
c++·算法·贪心算法·图论·最小生成树·kruskal
青 春 记 忆5 小时前
LeetCode 53. 最大子数组和|Python 解法详解
python·算法·leetcode
豆瓣鸡5 小时前
算法日记 - Day10
算法
戴西软件5 小时前
戴西CAxWorks.VPG车辆工程仿真软件技术解析(上)——安全仿真体系的自动化构建
运维·网络·数据库·人工智能·算法·安全·自动化
zander2585 小时前
4. 寻找两个正序数组的中位数:用分割点代替合并
数据结构·算法
Kisorge5 小时前
【电机控制器】 基于STSPIN32G4的FOC控制
stm32·嵌入式硬件·算法