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

链表22-30

22. 相交链表

给你两个单链表的头节点headAheadB,请你找出并返回两个单链表相交的起始节点。如果两个链表不存在相交节点,返回null

方法一:对齐后遍历

class Solution(object): def getIntersectionNode(self, headA, headB): num1,num2=0,0 cur_A=headA cur_B=headB while cur_A: num1+=1 cur_A=cur_A.next while cur_B: num2+=1 cur_B=cur_B.next num=num1-num2 cur_A=headA cur_B=headB if num>0: while num: cur_A=cur_A.next num-=1 else: while -num: num+=1 cur_B=cur_B.next while cur_A!=cur_B: cur_A=cur_A.next cur_B=cur_B.next return cur_A

方法二:哈希表

class Solution(object): def getIntersectionNode(self, headA, headB): S=set() node=headA while node: S.add(node) node=node.next node =headB while node: if node in S: return node node=node.next return node

23.反转链表

class Solution(object): def reverseList(self,head): vhead=ListNode() cur=head while cur: head=head.next cur.next=vhead.next vhead.next=cur cur=head return vhead.next

24. 回文链表

快慢指针

class Solution(object): def isPalindrome(self, head): val_head=ListNode(next=head) fast=val_head slow=val_head while fast and fast.next: slow=slow.next fast=fast.next.next val_head.next=None cur=slow.next slow.next=None while cur: new=cur.next cur.next=slow.next slow.next=cur cur=new cur=slow.next while cur and head: if cur.val==head.val: cur=cur.next head=head.next else: return False return True

25. 环形链表

快慢指针

class Solution(object): def hasCycle(self, head): slow=head fast=head while fast and fast.next: slow=slow.next fast=fast.next.next if slow==fast: return True return False

26. 环形链表 II

哈希表

class Solution(object): def detectCycle(self, head): my_dict={} index=0 while head: if head in my_dict: return head my_dict[head]=index index+=1 head=head.next return None

27. 合并两个有序链表

class Solution(object): def mergeTwoLists(self, list1, list2): val_head=ListNode() tail=val_head while list1 and list2: if list1.val<list2.val: cur=list1.next tail.next=list1 tail=list1 list1=cur else: cur=list2.next tail.next=list2 tail=list2 list2=cur if list1: tail.next=list1 if list2: tail.next=list2 return val_head.next

28. 两数相加

class Solution(object): def addTwoNumbers(self, l1, l2): carry = 0 dummy = ListNode(0) cur = dummy p, q = l1, l2 while p or q or carry: x = p.val if p else 0 y = q.val if q else 0 total = x + y + carry carry = total // 10 cur.next = ListNode(total % 10) cur = cur.next if p: p = p.next if q: q = q.next return dummy.next

29. 删除链表的倒数第 N 个结点

class Solution(object): def removeNthFromEnd(self, head, n): val_node=ListNode(next=head) fast=val_node slow=val_node for _ in range(n): fast=fast.next while fast.next: fast=fast.next slow=slow.next slow.next=slow.next.next return val_node.next

30. 两两交换链表中的节点

class Solution(object): def swapPairs(self, head): val_head=ListNode(next=head) cur=head while cur and cur.next: data=cur.val data_next=cur.next.val cur.val=data_next cur.next.val=data cur=cur.next.next return val_head.next
http://www.jsqmd.com/news/809395/

相关文章:

  • 漏洞审计技能进阶:从工具依赖到思维驱动的安全实战指南
  • 如何通过 curl 命令直接测试 Taotoken 的聊天补全接口
  • 2026年贵阳防雷工程避坑指南:甲级资质机构如何精准识别雷电隐患,保障企业资产安全 - 企业名录优选推荐
  • 老钱币邮票回收中心(三峡古玩城进门第一家)
  • ARP-Scan深度指南:从网络盲点到安全哨兵的蜕变之路
  • 河北选除臭鞋垫哪个牌子好 - 中媒介
  • 高低温试验箱口碑推荐:售后好的厂家推荐 - 品牌推荐大师
  • 从内核到应用:基于RV1126平台深度调试SIMCOM A7670C 4G模块上网全流程
  • 被格式逼到崩溃?Paperxie 一键搞定论文排版,把 3 天的活压缩到 3 分钟
  • 企业内网应用如何安全接入Taotoken并实现API访问控制与审计
  • 31.K 个一组翻转链表
  • C#新手向:搞懂“值类型”与“引用类型”及其对程序性能的影响
  • 给数字IC新手的STA避坑指南:从时序弧到建立时间,一次讲清楚
  • 【NotebookLM知识管理终极指南】:20年IT专家亲授5大高阶技巧,90%用户忽略的3个致命误区
  • 终极Windows与Office激活指南:KMS_VL_ALL_AIO完整教程
  • Scarf:智能网关加速软件包分发,提升开发者效率与项目洞察
  • 实验报告-树、二叉树与查找
  • 最低公共祖先 LCA
  • 被毕业论文逼到崩溃?Paperxie 这套本科论文通关流,直接把流程焊死了
  • 省下一台PLC的钱:海康VC3000工控机GPIO实战,替代小型PLC控制LED和开关
  • 5G工业物联网落地困境与务实路径:从技术鸿沟到场景破局
  • 2026天虹购物卡回收必看,三大平台折扣率与到账时间全解析 - 京顺回收
  • 国产钢研纳克直读光谱仪哪家好?南京艺御城仪器有限公司代理商服务采购指南 - 品牌推荐大师1
  • 5分钟快速上手:Windows安装Android应用的终极解决方案
  • 告别AD思维!Cadence 17.4 PCB封装绘制保姆级教程(以STM32 QFN48为例)
  • 2026宁波黄金回收门店盘点,价高人少不折腾 - 奢侈品回收测评
  • OpenClaw网关守护者:自动化监控、告警与自愈实践
  • 2026年喀什太阳能路灯、高杆灯采购指南:本地源头工厂一站式解决方案 - 优质企业观察收录
  • 3大核心场景重塑游戏串流体验:Sunshine开源串流服务器深度指南
  • 终极指南:如何绕过Cursor API限制,实现免费无限使用AI编程助手