234. 回文链表、Leetcode的Python实现

博客主页:🏆 看看是李XX还是李歘歘🏆

🌺每天分享一些包括但不限于计算机基础、算法等相关的知识点🌺

💗点关注不迷路,总有一些📖知识点📖是你想要的💗
⛽️今天的内容是 Leetcode 234. 回文链表 ⛽️💻💻💻

234. 回文链表

给你一个单链表的头节点 head ,请你判断该链表是否为回文链表。如果是,返回 true ;否则,返回 false 。

示例 1:

输入:head = [1,2,2,1]

输出:true

示例 2:

输入:head = [1,2]

输出:false

提示:

链表中节点数目在范围[1, 105] 内

0 <= Node.val <= 9

876. 链表的中间结点、Leetcode的Go实现_李歘歘的博客-CSDN博客

206. 反转链表、Leetcode的Go实现_李歘歘的博客-CSDN博客

使用206的反转方法,回文串反转后其值与原来一样:

注意:不可以在反转的链表和原链表是直接进行比较,因为链表是有地址的:

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]:
        tmp = None
        res = None
        while head is not None :
            tmp = head.next
            head.next = res
            res = head
            head = tmp
        return res

    def isPalindrome(self, head: ListNode) -> bool:
        orgList,revList = [],[]
        temp = head
        # 存储链表元素
        n = temp
        while n is not None:
            orgList.append(n.val)
            n = n.next
        # 反转链表
        reve = self.reverseList(head)
        # 存储链表元素
        m = reve
        while m is not None:
            revList.append(m.val)
            m = m.next
        # 链表不能直接比较其值(我们值关注链表中的val,当地址不同时也返回false)
        return  orgList == revList

先遍历链表,值保存在slice,后判断slice是否回文:此处省略

找链表中点,反转后半部分,对比:

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]:
        tmp = None
        res = None
        while head is not None :
            tmp = head.next
            head.next = res
            res = head
            head = tmp
        return res

    def isPalindrome(self, head: ListNode) -> bool:
        # 快慢指针找中点
        slow, fast = head,head
        while fast is not None and fast.next is not None :
            slow = slow.next
            fast = fast.next.next
        # 反转后半部分
        rev = self.reverseList(slow)
        # 链表直接对比前后两部分
        while rev is not None :
            if head.val != rev.val :
                return False
            head = head.next
            rev = rev.next
        return True

找到链表中点,并记录前半部分的值,对比中点后链表和前半段记录下的值 (注意链表总数奇偶):此处省略

相关推荐
飞翔的佩奇3 小时前
【完整源码+数据集+部署教程】表盘指针检测系统源码和数据集:改进yolo11-CA-HSFPN
python·yolo·计算机视觉·数据集·yolo11·表盘指针检测
larance3 小时前
SQLAlchemy 的异步操作来批量保存对象列表
数据库·python
搏博4 小时前
基于Python3.10.6与jieba库的中文分词模型接口在Windows Server 2022上的实现与部署教程
windows·python·自然语言处理·flask·中文分词
lxmyzzs5 小时前
pyqt5无法显示opencv绘制文本和掩码信息
python·qt·opencv
萧鼎6 小时前
Python pyzmq 库详解:从入门到高性能分布式通信
开发语言·分布式·python
yujkss7 小时前
Python脚本每天爬取微博热搜-终版
开发语言·python
yzx9910137 小时前
小程序开发APP
开发语言·人工智能·python·yolo
飞翔的佩奇7 小时前
【完整源码+数据集+部署教程】二维码与查找模式检测系统源码和数据集:改进yolo11-CSwinTransformer
python·yolo·计算机视觉·数据集·yolo11·二维码与查找模式检测
圣保罗的大教堂7 小时前
leetcode 2348. 全 0 子数组的数目 中等
leetcode
大霞上仙7 小时前
实现自学习系统,输入excel文件,能学习后进行相应回答
python·学习·excel