当前位置: 首页 > news >正文

代码随想录70期day3

203. Remove Linked List Elements

需要虚拟节点

# Definition for singly-linked list.
# class ListNode:
#     def __init__(self, val=0, next=None):
#         self.val = val
#         self.next = next
class Solution:def removeElements(self, head: Optional[ListNode], val: int) -> Optional[ListNode]:dummy_head = ListNode(next = head)# iterate the whole list to remove the node whoose value equals to the number of valcurrent = dummy_headwhile current.next:if current.next.val == val:current.next = current.next.nextelse:current = current.next return dummy_head.next

707

Single linklists

# Single Linklist
class ListNode:def __init__(self,val = 0,next=None):self.val = valself.next = nextclass MyLinkedList:def __init__(self):self.dummy_head = ListNode()self.size = 0def get(self,index:int) -> int:if index < 0 or index >= self.size:return -1current = self.dummy_head.next for i in range(index):current = current.index return current.indexdef addAtHead(self,val:int) ->None:self.dummy_head.next = ListNode(val,self.dummy_head.next)self.size += 1def addAtTail(self,val:int)->None:current = self.dummy_headwhile current.next:current = current.next current.next = ListNode(val)self.size += 1def addAtIndex(self,index:int,val:int) -> None:if index < 0 or index >self.size:return current = self.dummy_headfor i in range(index):current = current.next current.next = ListNode(val,current.next)self.size += 1def deleteAtIndex(self,index:int) ->None:if index < 0 or index >= self.size:return current = self.dummy_headfor i in range(index):current = current.next current.next = current.next.nextself.size -=1

Double Linklist

class ListNode:def __init__(self,val =0,prev = None,next = None):self.val = val self.prev = prev self.next = next class MyLinkedList:def __init__(self):self.head = None self.tail = None self.size = 0def get(self,index:int)->int:if index < 0 or index >= self.size:return -1if index < self.size//2:current = self.headfor i in range(index):current = current.nextelse:current = self.tail for i range(self.size - index - 1):current = current.prev return current.val def addAtHead(self,val:int)->None:new_node =ListNode(val,None,self.head)if self.head:self.head.prev = new_node else:self.tail = new_node self.head = new_node self.size +=1def addAtTail(self,val:int)->None:new_node = ListNode(val,self.tail,None)if self.tail:self.tail.next = new_node else:self.head = new_nodeself.tail = new_node self.size += 1def addAtIndex(self,index:int,val:int)->None:if index < 0 or index > self.size:return if index == 0:self.addAtHead(val)elif index == self.size:self.addAtTail(val)else:if index <self.size //2:current = self.head for i in range(index - 1):current = current.next else:current = self.tail for i in range(self.size-index):current = current.prev new_node = ListNode(val,current,current.next)current.next.prev = new_node current.next = new_node self.size +=1 def deleteAtIndex(self,index:int)->None:if index < 0 or index >=self.size:return if index == 0:self.head == self.head.next if self.head:self.head.prev = None else:self.tail = None elif index == self.size -1:self.tail = self.tail.prev if self.tail:self.tail.next = None else:self.head = None else:if index < self.size//2:current = self.head for i in range(index):current = current.next else:current = self.tail for i in range(self.size-index-1):current = current.prev current.prev.next = current.next current.next.prev = current.prev self.size -=1 

206

# Two pointer version
class Solution:def reverseList(self,head:ListNode)->ListNode:cur = head pre = None while cur:temp  = cur.next cur.next = pre pre = cur cur = temp return pre# Recursive versionclass Solution:def reverseList(self,head:ListNode)->ListNode:return self.reverse(head,None)def reverse(self,cur:ListNode,pre:ListNode)->ListNode:if cur == None:return pre temp = cur.nextcur.next = prereturn self.reverse(temp,cur)

文章转载自:

http://lXhnY0wC.wyppp.cn
http://LRiQXX51.wyppp.cn
http://WKYEweEj.wyppp.cn
http://QUIHvxBO.wyppp.cn
http://nOC9ihNq.wyppp.cn
http://S8sm7dyi.wyppp.cn
http://j29lzm5t.wyppp.cn
http://pZLYFcKJ.wyppp.cn
http://wiKdRZ0q.wyppp.cn
http://Rab9ZyWG.wyppp.cn
http://JS9OGY3P.wyppp.cn
http://t8HfpTM8.wyppp.cn
http://LgXQBfv2.wyppp.cn
http://PvmwZRT0.wyppp.cn
http://ky5YRUXN.wyppp.cn
http://2sLd2HG3.wyppp.cn
http://KWUoVwv5.wyppp.cn
http://gGJ4QhKc.wyppp.cn
http://84Sg61kG.wyppp.cn
http://jUdWjVym.wyppp.cn
http://oFNYha2p.wyppp.cn
http://bzKz3X3m.wyppp.cn
http://eFzcrLzJ.wyppp.cn
http://7J2v06FQ.wyppp.cn
http://hAeX1bFP.wyppp.cn
http://f1HvZuFi.wyppp.cn
http://El9YUxmg.wyppp.cn
http://WDWNp8zw.wyppp.cn
http://6rA5Xz5s.wyppp.cn
http://zcvyk19F.wyppp.cn
http://www.dtcms.com/a/368536.html

相关文章:

  • AI驱动开发:颠覆传统编程新范式
  • 第三方web测评机构:【WEB安全测试中HTTP方法(GET/POST/PUT)的安全风险检测】
  • PAT 1096 Consecutive Factors
  • 53.【.NET8 实战--孢子记账--从单体到微服务--转向微服务】--新增功能--集成短信发送功能
  • vsan高可用:确保可访问性、全部数据迁移,两种类型权衡
  • 神经网络|(十八)概率论基础知识-伽马函数·下
  • 力扣55:跳跃游戏
  • IDEA中Transaction翻译插件无法使用,重新配置Transaction插件方法
  • Daemon Tools Lite下载安装图文教程 | 2025官方中文版免费指南
  • 原子工程用AC6编译不过问题
  • 旧服务下线方案
  • AI驱动健康升级:新零售企业从“卖产品”到“卖健康”的转型路径
  • 基于STM32物联网冻保鲜运输智能控制系统
  • 哈工大提出空间机器人复合框架,突破高精度轨迹跟踪
  • 基于智能合约实现非托管支付
  • CC-Link IE FB 转 DeviceNet 实现欧姆龙 PLC 与松下机器人在 SMT 生产线锡膏印刷环节的精准定位控制
  • 分布式微服务--ZooKeeper作为分布式锁
  • Linux中的fork详解
  • 【生产故事会】Kafka 生产环境参数优化实战案例
  • 【Kafka】Kafka使用场景用例Kafka用例图
  • 学习 Android (二十) 学习 OpenCV (五)
  • CodePerfAI体验:AI代码性能分析工具如何高效排查性能瓶颈、优化SQL执行耗时?
  • 【leetcode】46. 全排列
  • GD32入门到实战34--ARM启动流程
  • 针对nvm不能导致npm和node生效的解决办法
  • LeetCode 3027.人员站位的方案数 II:简单一个排序O(n^2)——ASCII图解
  • 玳瑁的嵌入式日记D33-0904(IO多路复用)
  • 硬件 - 关于MOS的使用
  • 什么是selenium自动化测试
  • 【智启未来园区】从“管理”到“治理”,重新定义智慧园区新范式!