【算法刷题】总结规律 算法题目第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
相关推荐
无限进步_1 天前
【C语言】用队列实现栈:数据结构转换的巧妙设计
c语言·开发语言·数据结构·c++·链表·visual studio
Xの哲學1 天前
从硬中断到 softirq:Linux 软中断机制的全景解剖
linux·服务器·网络·算法·边缘计算
生信碱移1 天前
单细胞空转CNV分析工具:比 inferCNV 快10倍?!兼容单细胞与空转的 CNV 分析与聚类,竟然还支持肿瘤的亚克隆树构建!
算法·机器学习·数据挖掘·数据分析·聚类
Brduino脑机接口技术答疑1 天前
TDCA 算法在 SSVEP 场景中:Padding 的应用对象与工程实践指南
人工智能·python·算法·数据分析·脑机接口·eeg
keep_learning1111 天前
Z-Image模型架构全解析
人工智能·算法·计算机视觉·大模型·多模态
点云SLAM1 天前
Boost中Graph模块中boost::edge_capacity和boost::edge_capacity_t
数据库·算法·edge·图论·最大团·最大流算法·boost库使用
lihaihui19911 天前
asan 内存问题分析
算法
算法与编程之美1 天前
探索不同的损失函数对分类精度的影响.
人工智能·算法·机器学习·分类·数据挖掘
H_BB1 天前
leetcode160:相交链表
数据结构·算法·链表
前端小L1 天前
贪心算法专题(十五):借位与填充的智慧——「单调递增的数字」
javascript·算法·贪心算法