news 2026/5/13 10:12:15

二、链表刷题

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
二、链表刷题

1
移除链表元素
题目链接/文章讲解/视频讲解

defremoveElements(self,head:Optional[ListNode],val:int)->Optional[ListNode]:dummyNode=ListNode(next=head)cur=dummyNodewhilecur.next:ifcur.next.val==val:cur.next=cur.next.nextelse:cur=cur.nextreturndummyNode.next

2设计链表
文章讲解

classListNode:def__init__(self,val=0,next=None):self.val=val self.next=nextclassMyLinkedList:def__init__(self):self.dummy_head=ListNode()self.size=0defget(self,index:int)->int:ifindex<0orindex>=self.size:return-1current=self.dummy_head.nextforiinrange(index):current=current.nextreturncurrent.valdefaddAtHead(self,val:int)->None:self.dummy_head.next=ListNode(val,self.dummy_head.next)self.size+=1defaddAtTail(self,val:int)->None:current=self.dummy_headwhilecurrent.next:current=current.nextcurrent.next=ListNode(val)self.size+=1defaddAtIndex(self,index:int,val:int)->None:ifindex<0orindex>self.size:returncurrent=self.dummy_headforiinrange(index):current=current.nextcurrent.next=ListNode(val,current.next)self.size+=1defdeleteAtIndex(self,index:int)->None:ifindex<0orindex>=self.size:returncurrent=self.dummy_headforiinrange(index):current=current.nextcurrent.next=current.next.nextself.size-=1

3.反转链表
文章讲解

defreverseList(self,head:ListNode)->ListNode:cur=head pre=Nonewhilecur:temp=cur.nextcur.next=pre pre=cur cur=tempreturnpre

4.两两交换链表中的节点
文章讲解

defswapPairs(self,head:Optional[ListNode])->Optional[ListNode]:dummyNode=ListNode(0,head)nodeOne=dummyNode nodeTwo=headwhilenodeTwoandnodeTwo.next:#一定要两个条件,否则会报错temp=nodeTwo.next.nextnodeOne.next=nodeTwo.nextnodeTwo.next.next=nodeTwo nodeTwo.next=temp nodeOne=nodeTwo nodeTwo=temp# nodethree = nodeTwo.next #nodeTwo为0时会报错,所以不要初始化,可以直接用。returndummyNode.next

5.删除链表的倒数第N个节点
文章讲解

dummyNode=ListNode(0,head)cur=dummyNode pre=dummyNodeforiinrange(n):pre=pre.nextwhilepre.next:cur=cur.nextpre=pre.nextcur.next=cur.next.nextreturndummyNode.next

6.链表相交
文章讲解

lenA=1lenB=1cur=headAwhilecur:cur=cur.nextlenA+=1cur=headBwhilecur:cur=cur.nextlenB+=1iflenA<lenB:# 让curB为最长链表的头,lenB为其长度headA,headB=headB,headA lenA,lenB=lenB,lenAforiinrange(lenA-lenB):headA=headA.nextwhileheadA:ifheadA==headB:returnheadA headA=headA.nextheadB=headB.nextreturnNone

7.环形链表II
文章讲解

defdetectCycle(self,head:Optional[ListNode])->Optional[ListNode]:fast=slow=headwhilefastandfast.next:fast=fast.next.nextslow=slow.nextiffast==slow:slow=headwhileslow!=fast:slow=slow.nextfast=fast.nextreturnslowreturnNone
版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2026/5/13 10:11:27

低成本传感器动态校准:SenDaL框架原理与应用

1. 低成本传感器校准的行业痛点与SenDaL解决方案在智能家居和工业物联网领域&#xff0c;我们经常面临一个尴尬的境地&#xff1a;高精度传感器价格昂贵难以大规模部署&#xff0c;而低成本传感器的数据质量又令人担忧。以PM2.5监测为例&#xff0c;专业级β射线传感器的价格可…

作者头像 李华
网站建设 2026/5/13 10:10:15

AI辅助开发中的文档即代码:八层文档体系与对抗性审查实践

1. 项目概述&#xff1a;一个为AI辅助开发而生的“文档即代码”工程模板如果你和我一样&#xff0c;在过去一年里深度使用Cursor、Claude Code这类AI编程工具&#xff0c;那你一定经历过这种“信任崩塌”的时刻&#xff1a;项目初期&#xff0c;AI生成的文档看起来条理清晰、逻…

作者头像 李华
网站建设 2026/5/13 10:07:22

技术社区活动策划指南:从工程师幽默感谈社区参与与运营

1. 从一则旧闻聊起&#xff1a;工程师的幽默感与社区参与翻看十多年前的行业旧闻&#xff0c;总能发现一些有趣的时代切片。2010年8月&#xff0c;EE Times旗下的EE Life栏目发起了一场卡通配文大赛。规则很简单&#xff1a;为一幅工程师主题的漫画构思一个有趣的标题&#xff…

作者头像 李华
网站建设 2026/5/13 10:07:18

《C语言学习:数据类型》8

写在前面&#xff1a;本笔记为个人学习各平台C语言系列课程所作&#xff0c;仅供交流学习&#xff0c;不得作他用。1. 背景知识C语言的变量在使用前需要先定义&#xff0c;确定其类型。C语言的发展有两个方向&#xff1a;总体而言&#xff1a;早期的C语言面向底层&#xff0c;强…

作者头像 李华