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
相关推荐
倒头就睡的小比特5 天前
算法竞赛C++常用的STL
c++·算法
默_笙5 天前
🍙 给每个请求过安检:FastAPI 是怎么把校验写进类型注解的
python
小羊没烦恼!5 天前
初探性能优化——2个月到4小时的性能提升
java·开发语言·windows·算法·c#
qq_426003965 天前
启动playwright录制codegen生成自动化测试脚本
python·自动化
虎头金猫5 天前
4K 视频总卡在公网带宽?用 N1 + OpenList 把网盘播放链路重新理顺
运维·服务器·网络·python·容器·beautifulsoup·pandas
长沙三为智能科技5 天前
家政小程序开发从0到上线:五阶段交付流程与验收清单
python
伞伞悦读5 天前
【第38期】Python 模块与包详解:import、from、模块搜索路径、包结构和 __init__
开发语言·python
猎头南楼5 天前
知识社区推荐系统实践:新用户冷启动与长短期兴趣建模的挑战 资深推荐算法工程师
人工智能·深度学习·算法·机器学习
只睡四小时5 天前
Canvas 弹道联机实战:700 行 + 固定时间步长
python·websocket·html5·游戏开发·canvas
旖旎夜光5 天前
力控面试题 01.01: 判定字符是否唯一(位运算) —— 题解
c++·学习·算法·leetcode·力控