1移除链表元素题目链接/文章讲解/视频讲解defremoveElements(self,head:Optional[ListNode],val:int)-Optional[ListNode]:dummyNodeListNode(nexthead)curdummyNodewhilecur.next:ifcur.next.valval:cur.nextcur.next.nextelse:curcur.nextreturndummyNode.next2设计链表文章讲解classListNode:def__init__(self,val0,nextNone):self.valval self.nextnextclassMyLinkedList:def__init__(self):self.dummy_headListNode()self.size0defget(self,index:int)-int:ifindex0orindexself.size:return-1currentself.dummy_head.nextforiinrange(index):currentcurrent.nextreturncurrent.valdefaddAtHead(self,val:int)-None:self.dummy_head.nextListNode(val,self.dummy_head.next)self.size1defaddAtTail(self,val:int)-None:currentself.dummy_headwhilecurrent.next:currentcurrent.nextcurrent.nextListNode(val)self.size1defaddAtIndex(self,index:int,val:int)-None:ifindex0orindexself.size:returncurrentself.dummy_headforiinrange(index):currentcurrent.nextcurrent.nextListNode(val,current.next)self.size1defdeleteAtIndex(self,index:int)-None:ifindex0orindexself.size:returncurrentself.dummy_headforiinrange(index):currentcurrent.nextcurrent.nextcurrent.next.nextself.size-13.反转链表文章讲解defreverseList(self,head:ListNode)-ListNode:curhead preNonewhilecur:tempcur.nextcur.nextpre precur curtempreturnpre4.两两交换链表中的节点文章讲解defswapPairs(self,head:Optional[ListNode])-Optional[ListNode]:dummyNodeListNode(0,head)nodeOnedummyNode nodeTwoheadwhilenodeTwoandnodeTwo.next:#一定要两个条件否则会报错tempnodeTwo.next.nextnodeOne.nextnodeTwo.nextnodeTwo.next.nextnodeTwo nodeTwo.nexttemp nodeOnenodeTwo nodeTwotemp# nodethree nodeTwo.next #nodeTwo为0时会报错所以不要初始化可以直接用。returndummyNode.next5.删除链表的倒数第N个节点文章讲解dummyNodeListNode(0,head)curdummyNode predummyNodeforiinrange(n):prepre.nextwhilepre.next:curcur.nextprepre.nextcur.nextcur.next.nextreturndummyNode.next6.链表相交文章讲解lenA1lenB1curheadAwhilecur:curcur.nextlenA1curheadBwhilecur:curcur.nextlenB1iflenAlenB:# 让curB为最长链表的头lenB为其长度headA,headBheadB,headA lenA,lenBlenB,lenAforiinrange(lenA-lenB):headAheadA.nextwhileheadA:ifheadAheadB:returnheadA headAheadA.nextheadBheadB.nextreturnNone7.环形链表II文章讲解defdetectCycle(self,head:Optional[ListNode])-Optional[ListNode]:fastslowheadwhilefastandfast.next:fastfast.next.nextslowslow.nextiffastslow:slowheadwhileslow!fast:slowslow.nextfastfast.nextreturnslowreturnNone