news 2026/6/12 10:13:41

链表22-30

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
链表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
版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2026/6/12 10:11:18

省下一台PLC的钱:海康VC3000工控机GPIO实战,替代小型PLC控制LED和开关

省下一台PLC的钱&#xff1a;海康VC3000工控机GPIO实战&#xff0c;替代小型PLC控制LED和开关 在中小型自动化项目中&#xff0c;成本控制往往是工程师们最头疼的问题之一。当项目预算有限时&#xff0c;如何利用现有设备实现功能扩展&#xff0c;成为考验技术灵活性的关键。海…

作者头像 李华
网站建设 2026/5/13 14:49:00

Jira、ONES、ClickUp 对比:哪款研发管理软件更适合中国研发团队?

快速迭代的互联网和软件行业&#xff0c;研发团队的效率管理工具几乎决定了产品交付的速度与质量。研发管理软件不仅是“任务分派”的工具&#xff0c;更是团队 需求管理、版本迭代、缺陷跟踪、研发效能度量 的基础设施。 目前市面上主流的研发管理软件众多&#xff0c;不同工…

作者头像 李华
网站建设 2026/5/13 14:46:31

如何免费获取Book118文档?这个Java工具让你轻松下载完整PDF

如何免费获取Book118文档&#xff1f;这个Java工具让你轻松下载完整PDF 【免费下载链接】book118-downloader 基于java的book118文档下载器 项目地址: https://gitcode.com/gh_mirrors/bo/book118-downloader 你是否曾经在Book118网站上找到了一份急需的学习资料&#x…

作者头像 李华
网站建设 2026/5/13 14:42:23

初创团队如何利用Taotoken统一管理多个AI模型的API调用与成本

&#x1f680; 告别海外账号与网络限制&#xff01;稳定直连全球优质大模型&#xff0c;限时半价接入中。 &#x1f449; 点击领取海量免费额度 初创团队如何利用Taotoken统一管理多个AI模型的API调用与成本 对于许多初创团队而言&#xff0c;大模型能力已成为产品开发与运营的…

作者头像 李华