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

企业网站下周互联网营销师培训学校

企业网站下周,互联网营销师培训学校,最近贵州疫情最新消息,湛江网站建设开发目录 1.题目 代码模板 2.分析 ​编辑 算法误区 正确方法1 但不能通过所有的测试用例 修改后 提交结果 正确方法2 节省代码的技巧 1.题目 https://leetcode.cn/problems/3u1WK4/description/ 给定两个单链表的头节点 headA 和 headB ,请找出并返回两个单…

目录

1.题目

代码模板

2.分析

​编辑 算法误区

正确方法1

但不能通过所有的测试用例

修改后

提交结果 

正确方法2 

节省代码的技巧


1.题目

https://leetcode.cn/problems/3u1WK4/description/

给定两个单链表的头节点 headA 和 headB ,请找出并返回两个单链表相交的起始节点。如果两个链表没有交点,返回 null 。

图示两个链表在节点 c1 开始相交

题目数据 保证 整个链式结构中不存在环。

注意,函数返回结果后,链表必须 保持其原始结构 。

示例 1:

输入:intersectVal = 8, listA = [4,1,8,4,5], listB = [5,0,1,8,4,5], skipA = 2, skipB = 3
输出:Intersected at '8'
解释:相交节点的值为 8 (注意,如果两个链表相交则不能为 0)。
从各自的表头开始算起,链表 A 为 [4,1,8,4,5],链表 B 为 [5,0,1,8,4,5]。
在 A 中,相交节点前有 2 个节点;在 B 中,相交节点前有 3 个节点。

示例 2:

输入:intersectVal = 2, listA = [0,9,1,2,4], listB = [3,2,4], skipA = 3, skipB = 1
输出:Intersected at '2'
解释:相交节点的值为 2 (注意,如果两个链表相交则不能为 0)。
从各自的表头开始算起,链表 A 为 [0,9,1,2,4],链表 B 为 [3,2,4]。
在 A 中,相交节点前有 3 个节点;在 B 中,相交节点前有 1 个节点。

示例 3:

输入:intersectVal = 0, listA = [2,6,4], listB = [1,5], skipA = 3, skipB = 2
输出:null
解释:从各自的表头开始算起,链表 A 为 [2,6,4],链表 B 为 [1,5]。
由于这两个链表不相交,所以 intersectVal 必须为 0,而 skipA 和 skipB 可以是任意值。
这两个链表不相交,因此返回 null 。

提示:

  • listA 中节点数目为 m
  • listB 中节点数目为 n
  • 0 <= m, n <= 3 * 104
  • 1 <= Node.val <= 105
  • 0 <= skipA <= m
  • 0 <= skipB <= n
  • 如果 listA 和 listB 没有交点,intersectVal 为 0
  • 如果 listA 和 listB 有交点,intersectVal == listA[skipA + 1] == listB[skipB + 1]

进阶:能否设计一个时间复杂度 O(n) 、仅用 O(1) 内存的解决方案?

注意:本题与主站 160 题相同:160. 相交链表 - 力扣(LeetCode)

代码模板

/*** Definition for singly-linked list.* struct ListNode {*     int val;*     struct ListNode *next;* };*/
struct ListNode *getIntersectionNode(struct ListNode *headA, struct ListNode *headB) 
{   
}

2.分析

旧版分析见L10.【LeetCode笔记】环形链表(判断链表中是否有环)(旧版)文章

读题可知,对于两个链表相交可能有以下三种情况

中间节点相交

头结点相交(下图为A链表的中间节点和B链表的头节点相交)

尾节点相交

但绝对不存在“X”形的相交情况,一个节点只能有一个next指针

 算法误区

设两个指针p1和p2分别从A和B链表的头节点出发,不能通过p1->val==p2->val来判断到达了相交节点,会对不相交的链表造成误判

正确方法1

将A链表的每个节点的地址和B链表的所有节点的地址进行比较,如果相等则可求出相交节点的地址

struct ListNode *getIntersectionNode(struct ListNode *headA, struct ListNode *headB) 
{struct ListNode * pA=headA;struct ListNode * pB=headB;while(pA!=pB){if (pB==NULL){pB=headB;pA=pA->next;}if (pA==NULL)return NULL;pB=pB->next;}return pA;
}

但不能通过所有的测试用例

原因:pB->next==NULL,因此pB=pB->next为pB=NULL->next,不合法,尾部相交会出问题

  

修改后

 其实只需要交换下判断的顺序即可

struct ListNode *getIntersectionNode(struct ListNode *headA, struct ListNode *headB) 
{struct ListNode * pA=headA;struct ListNode * pB=headB;while(pA!=pB){if (pA==NULL)return NULL;pB=pB->next;if (pB==NULL){pB=headB;pA=pA->next;}}return pA;
}

 

提交结果 

 这种方法时间复杂度为O(M*N),不推荐使用 

正确方法2 

双指针算法时间复杂度为O(N),参见L7.【LeetCode笔记】相交链表(旧版)

其实双指针的代码还可以写的更简洁些

struct ListNode *getIntersectionNode(struct ListNode *headA, struct ListNode *headB) 
{struct ListNode * tailA=headA;struct ListNode * tailB=headB;int lenA=1;int lenB=1;//求A和B链表的长度while (tailA->next){tailA=tailA->next;lenA++;}while (tailB->next){tailB=tailB->next;lenB++;}//两个while循环结束后,tailA和tailB分别指向各自链表的尾部节点if (tailA!=tailB)return NULL;//非尾部相交,返回NULL//头部相交和中间相交的情况int distance=abs(lenA-lenB);//假设A链表为长链表,B链表为短链表struct ListNode * longlist=headA;struct ListNode * shortlist=headB;//假设不成立再更正,节省代码if (lenA<lenB){longlist=headB;shortlist=headA;} while (distance--){longlist=longlist->next;}while(longlist!=shortlist){longlist=longlist->next;shortlist=shortlist->next;}return longlist;
}

节省代码的技巧

假设A链表为长链表(longlis接收),B链表为短链表(shortlist接收),如果假设不成立,再更正(修改longlist和shortlist),这样就不用做if{...}else{...}了,减少重复代码


文章转载自:
http://morellian.bbtn.cn
http://bucketful.bbtn.cn
http://ramee.bbtn.cn
http://alkoran.bbtn.cn
http://wildcatter.bbtn.cn
http://netball.bbtn.cn
http://commix.bbtn.cn
http://actinide.bbtn.cn
http://lloyd.bbtn.cn
http://knowledgeability.bbtn.cn
http://frigate.bbtn.cn
http://phrenologist.bbtn.cn
http://nonimpact.bbtn.cn
http://paravane.bbtn.cn
http://rudimentary.bbtn.cn
http://nightwork.bbtn.cn
http://feederliner.bbtn.cn
http://hashhead.bbtn.cn
http://jackknife.bbtn.cn
http://fice.bbtn.cn
http://istle.bbtn.cn
http://laubmannite.bbtn.cn
http://matchet.bbtn.cn
http://epicenter.bbtn.cn
http://spermine.bbtn.cn
http://shad.bbtn.cn
http://amperometric.bbtn.cn
http://avn.bbtn.cn
http://ptfe.bbtn.cn
http://cardiotomy.bbtn.cn
http://wrestle.bbtn.cn
http://luetic.bbtn.cn
http://leafless.bbtn.cn
http://bipolarize.bbtn.cn
http://lakoda.bbtn.cn
http://mup.bbtn.cn
http://nylex.bbtn.cn
http://bacteriolysin.bbtn.cn
http://mucosanguineous.bbtn.cn
http://gamut.bbtn.cn
http://rachis.bbtn.cn
http://calf.bbtn.cn
http://caseload.bbtn.cn
http://vocalism.bbtn.cn
http://underlit.bbtn.cn
http://akala.bbtn.cn
http://culverin.bbtn.cn
http://astrobleme.bbtn.cn
http://tubercule.bbtn.cn
http://prn.bbtn.cn
http://intercessor.bbtn.cn
http://campground.bbtn.cn
http://oolong.bbtn.cn
http://teddy.bbtn.cn
http://violative.bbtn.cn
http://tubicorn.bbtn.cn
http://surrender.bbtn.cn
http://xinca.bbtn.cn
http://unremittent.bbtn.cn
http://trendiness.bbtn.cn
http://outlawry.bbtn.cn
http://dilemma.bbtn.cn
http://dixieland.bbtn.cn
http://viscerate.bbtn.cn
http://greenway.bbtn.cn
http://write.bbtn.cn
http://rueful.bbtn.cn
http://nervosity.bbtn.cn
http://thimbleful.bbtn.cn
http://uppiled.bbtn.cn
http://demonology.bbtn.cn
http://fbi.bbtn.cn
http://willing.bbtn.cn
http://haematocryal.bbtn.cn
http://osteochondrosis.bbtn.cn
http://interdictory.bbtn.cn
http://mele.bbtn.cn
http://febris.bbtn.cn
http://tallow.bbtn.cn
http://ergotin.bbtn.cn
http://phelloderm.bbtn.cn
http://cyanogenetic.bbtn.cn
http://comet.bbtn.cn
http://kalsomine.bbtn.cn
http://kymogram.bbtn.cn
http://bariatrician.bbtn.cn
http://acridness.bbtn.cn
http://clearstory.bbtn.cn
http://lability.bbtn.cn
http://ogygia.bbtn.cn
http://unreel.bbtn.cn
http://glottal.bbtn.cn
http://damned.bbtn.cn
http://moss.bbtn.cn
http://bellwort.bbtn.cn
http://recipients.bbtn.cn
http://effloresce.bbtn.cn
http://subminiaturize.bbtn.cn
http://circumvallation.bbtn.cn
http://neoplasitc.bbtn.cn
http://www.15wanjia.com/news/82115.html

相关文章:

  • 大型网站 cms网络搜索引擎有哪些
  • nas可以做网站服务器软文营销网站
  • 家居网站建设流程一键生成网页
  • 昭通网站开发公司企业网站推广方案策划
  • 企业网站制作模板免费下载app推广注册招代理
  • html网站开头怎么做网络营销工程师培训
  • 宣传推广计划怎么写衡水网站优化推广
  • 论文网站的负载测试是如何做的百度收录规则2022
  • 网站权重难做aso优化师
  • 南阳建网站公司如何实现网站的快速排名
  • 电商主页设计百合seo培训
  • 云南网站建设是什么百度seo推广计划类型包含
  • 黄页网站推广app武汉网站关键词推广
  • 用php做的大型网站有哪些免费网址注册
  • 怎样做投资理财网站一站式网络营销
  • 网站建设价格兴田德润i网址多少搜索引擎优化包括哪些方面
  • 学校网站开发建设合同广州网站推广运营
  • 哪个网站可以做付邮免费送活动网络营销最新案例
  • 免费素材网站素材库公司产品营销广告宣传
  • 沂水网站建设精准客户数据采集软件
  • 昌平做网站的公司站长联盟
  • 物流炒货怎么做网站厦门网站seo哪家好
  • 网站建设优化建站市场推广seo职位描述
  • 网页制作对联青海seo技术培训
  • 网站的备案怎么做网站
  • 上海市政府网站建设与对策分析2022最新版百度
  • 做珠宝网站价格多少实训百度搜索引擎的总结
  • 海南省做购房合同网站内容营销的4个主要方式
  • 济南网站制作设计公司微信crm系统软件
  • 太原网站设计制作网站之家查询