leetcode hot 100

234.回文链表

主要是单链表不能倒着读,所以放数组里,然后双指针

python 复制代码
# Definition for singly-linked list.
# class ListNode:
#     def __init__(self, val=0, next=None):
#         self.val = val
#         self.next = next
class Solution:
    def isPalindrome(self, head: Optional[ListNode]) -> bool:
        num=[]
        p=head
        while p!=None:
            num.append(p.val)
            p=p.next
        flag=False
        if head!=None:
            flag=True
        i=0
        while i<len(num)//2:
            if num[i]!=num[len(num)-1-i]:
                flag=False
            i+=1
        return flag

        

141. 环形链表

秒了。

python 复制代码
# Definition for singly-linked list.
# class ListNode:
#     def __init__(self, x):
#         self.val = x
#         self.next = None

class Solution:
    def hasCycle(self, head: Optional[ListNode]) -> bool:
        hashtable=dict()
        p=head
        flag=False
        while p!=None:
            if p in hashtable:
                flag=True
                break
            hashtable[p]=1
            p=p.next
        return flag

142.环形链表2

继续哈希,秒了

python 复制代码
class Solution:
    def detectCycle(self, head: Optional[ListNode]) -> Optional[ListNode]:
        hashtable=dict()
        p=head
        cnt=0
        while p!=None:
            if p in hashtable:
                return p
            hashtable[p]=cnt
            p=p.next
            cnt+=1
        return None
相关推荐
SeanQhl36 分钟前
用 Python 给长文做排版体检:图够不够、段落是不是太长
python·自动化·markdown·代码规范·写作工具
SeanQhl41 分钟前
中文 Windows 上跑 Python 定时任务——编码、路径、控制台三个坑
windows·python·编码·效率工具·定时任务
积流成海3 小时前
Agent演化史(十):2027年它会消失吗?(终章)
python·agent·智能体·大模型应用
liulilittle3 小时前
认识麻将 —— 流水及对账
算法·游戏·lua·对账·流水·资金·玩法
专注仿真4 小时前
仿真智能体训练:强化学习中的奖励塑形算法与实战
python·强化学习·afsim智能体·仿真训练·afsim智能体训练
3095566665 小时前
股票接口日期参数为什么不起作用:五组请求对照与逐字节比对排查笔记
服务器·数据库·笔记·python·量化·api接口·股票接口
JonaHu5 小时前
GPT Image 2.6 改图交付防串版:文件指纹、批准清单与下载验收
图像处理·人工智能·python
Java码农也是农7 小时前
Rust、Java、Python、C++ 有什么区别?从 CPU 执行到内存管理
java·python·rust
郝学胜-神的一滴7 小时前
Effective Python 条款44:善用属性注解
开发语言·c++·vscode·python·算法·stl
钱栈up11 小时前
两台机器、一个 AI 记忆库:101 秒把 137 个文件对齐
爬虫·python·数据分析