【算法刷题】总结规律 算法题目第2讲 [234] 回文链表,因为深浅拷贝引出的bug

配合b站视频讲解食用更佳:https://www.bilibili.com/video/BV1vW4y1P7V7

核心提示:好几道题是处理有序数组的!

适合人群:考研/复试/面试

解决痛点:1. 刷了就忘 2.换一道相似的题就不会

学完后会输出:对每类题目的框架

python 复制代码
#
# @lc app=leetcode.cn id=234 lang=python3
#
# [234] 回文链表
#
from typing import Optional
import copy
class ListNode:
    def __init__(self, val=0, next=None):
        self.val = val
        self.next = next
# @lc code=start
# Definition for singly-linked list.
# class ListNode:
#     def __init__(self, val=0, next=None):
#         self.val = val
#         self.next = next
class Solution:
    def reserve(self,head:Optional[ListNode])->Optional[ListNode]:
        if (not head) or (not head.next):
            return head
        last = self.reserve(head.next)
        head.next.next = head
        head.next = None
        return last
    def isPalindrome(self, head: Optional[ListNode]) -> bool:
        head1 = copy.deepcopy(head)
        res = self.reserve(head1)
        while head and res:
            if head.val == res.val:
                head = head.next
                res = res.next
            else:
                return False
        return True
        
# @lc code=end
# 1,1,2,1
n0 = ListNode(1)
n1 = ListNode(1)
n2 = ListNode(2)
n3 = ListNode(1)
n0.next = n1
n1.next = n2
n2.next = n3
Solution().isPalindrome(n0)

判断链表是否是回文链表的问题,对应力扣234题:题目连接https://leetcode.cn/problems/palindrome-linked-list/description/

这道题我采用的思路是,翻转链表,然后和原链表挨个节点做比较。

但是写出了bug,

bug 在这里,是深浅拷贝的问题

res = self.reserve(head) 是不行的,因为head会被reserve改写,然后浅拷贝也是不行的,会报错。深拷贝是对的。

复制代码
  head1 = copy.deepcopy(head)
  res = self.reserve(head1)

对于简单的 object,例如不可变对象(数值,字符串,元组),用 shallow copy 和 deep copy 没区别

复杂的 object, 如 list 中套着 list 的情况,shallow copy 中的 子list,并未从原 object 真的「独立」出来。也就是说,如果你改变原 object 的子 list 中的一个元素,你的 copy 就会跟着一起变。这跟我们直觉上对「复制」的理解不同。

一个很考察基本功,但是很赞的解法:

step1. 找中点

step2. 翻转中点后面的链表

step3. 比较left 和 right

python 复制代码
    def isPalindrome(self, head: Optional[ListNode]) -> bool:
        
        if not (head and head.next):
            return True
        # 找中点
        slow,fast = head,head
        while fast and fast.next:
            fast = fast.next.next
            slow = slow.next
        if fast:
            slow = slow.next
        left,right= head,self.reserve(slow)
        while left and right:
            if left.val != right.val:
                return False
            left = left.next
            right = right.next
        
        return True
相关推荐
强哥聊算力几秒前
《算力江湖·Intel帝国往事》第三集:安迪·格鲁夫的铁血时代:Intel为什么能把对手逼到墙角?
运维·人工智能·科技·算法·ai·gpu算力
Shaoshing19 分钟前
LeetCode100 11. 盛最多水的容器
算法·leetcode
8年车载测试23 分钟前
汽车五大域详解:动力、底盘、车身、座舱与智驾域
汽车·bug·jira·uds诊断·ecu刷写测试·canoe工具
骑着蜗牛撵大象3271 小时前
558 万美元训出 671B 模型:DeepSeek-V3 算法与系统的协同拆解
分布式·算法·moe·deepseek-v3·mla·fp8训练
Eyfcom1 小时前
〖阶段性回顾〗物流系统架构与算法的 Day 1-5 知识图谱与实战答疑
算法·系统架构·知识图谱·物流管理系统·快递管理系统
xunberg1 小时前
【Agent 生产化手记-02】Agent 怎么向人提问
人工智能·算法·架构
海绵宝宝转agent1 小时前
Leetcode二叉树算法个人理解
笔记·学习·算法·leetcode·深度优先
pjj198541 小时前
YOLO 目标检测算法系列梳理:从 V1 到 V5 的原理演进
算法·yolo·目标检测
二十七日秋2 小时前
算法刷题笔记「Day 1|四道 OJ 入门题里的深坑」
c语言·汇编·数据结构·c++·笔记·python·算法
晚风叙码2 小时前
Linux 进程概念(下):程序地址空间与虚拟内存
linux·服务器·算法