Leetcode:Intersection of Two Linked Lists
题目大意:找两个链表的第一个交点。
这里先给出一些链表相交基础的说明:
对于链表X,用X[i]表示链表X的第i个元素,L(X)表示X的长度。
性质1:如果X[i]等于Y[j],那么可以推出X[i+1]等于Y[j+1]。
性质2:如果X[i]等于Y[j]且X[i-1]不等于Y[j-1],那么X和Y的第一个交点为X[i]。
性质3:如果X[i]=Y[j],那么L(X)-i=L(Y)-j。
这个题目有一个非常有趣的解法(参考自https://discuss.leetcode.com/topic/28067/java-solution-without-knowing-the-difference-in-len?page=1)。直接上代码:
public class Solution {public ListNode getIntersectionNode(ListNode headA, ListNode headB) {ListNode a = headA;ListNode b = headB;while(a != b){a = a == null ? headB : a.next;b = b == null ? headA : b.next;}return a;} }
这里通过两个指针a,b分别遍历链表headA与headB,在a遍历完headA所代表的链表后则继续遍历headB,同样b遍历完headB代表的链表后则继续遍历headA。
先说明while循环必定会终止。假设n为headA的链表长度,m为headB的链表长度,那么在第n+m次while循环时,a和b将分别抵达headB和headA的尾部,即均为null。因此我们能保证while最多循环不超过n+m次。
在知道了循环在n+m次执行必定终止,那么很容易分析出时间复杂度为O(n+m),同样由于只使用了两个指针,因此空间复杂度为O(1)。
最后说明算法的正确性,即当while循环退出时,a和b代表headA和headB二者第一个相交的结点。可以分2种情况进行讨论:
n等于m时:由于n等于m,因此我们能保证第一个相交的结点必定能在前面n步内发现(因为第n步a和b同时为null)。当第i次while循环退出时,显然满足headA[i]=headB[i]且headA[i-1]!=headB[i-1],故a=headA[i]为第一个交点。
n不等于m时:记s=max(n,m),我们能保证在前面s次while循环内不可能满足a==b(若存在i<s满足headA[i]=headB[i],由性质3可以推出L(headA)=L(headB))。因此while会在a和b分别遍历headB和headA的过程中退出。当从while循环退出时,由于性质2的前提被满足,故得到的是第一个交点。
转载于:https://www.cnblogs.com/dalt/p/7806236.html
Leetcode:Intersection of Two Linked Lists相关推荐
- [LeetCode] Intersection of Two Linked Lists 求两个链表的交点
Write a program to find the node at which the intersection of two singly linked lists begins. For ex ...
- LeetCode Intersection of Two Linked Lists
Write a program to find the node at which the intersection of two singly linked lists begins. For ex ...
- leetcode. Intersection of Two Linked Lists
Write a program to find the node at which the intersection of two singly linked lists begins. For ex ...
- LeetCode : Intersection of Two Linked Lists
Write a program to find the node at which the intersection of two singly linked lists begins. For ex ...
- LeetCode(160): Intersection of Two Linked Lists
Intersection of Two Linked Lists: Write a program to find the node at which the intersection of two ...
- [LeetCode]--160. Intersection of Two Linked Lists
Write a program to find the node at which the intersection of two singly linked lists begins. For ex ...
- C#LeetCode刷题之#160-相交链表(Intersection of Two Linked Lists)
问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3824 访问. 编写一个程序,找到两个单链表相交的起始节点. 例如 ...
- leetcode python3 简单题160. Intersection of Two Linked Lists
1.编辑器 我使用的是win10+vscode+leetcode+python3 环境配置参见我的博客: 链接 2.第一百六十题 (1)题目 英文: Write a program to find t ...
- 【leetcode】Intersection of Two Linked Lists
Write a program to find the node at which the intersection of two singly linked lists begins. For ex ...
最新文章
- 计算机ppt继续教育,泰州市专业技术人员继续教育公需科目PPT2010试卷及答案
- 6,ORM组件XCode(撬动千万级数据)
- 绘制半圆_AI教程/绘制雪人插画
- linux 阻止 复位命令,linux防误删操作(使用safe-rm;使用mv命令删除文件)
- JAVA IOC及代理模式
- DB2计划三招“破甲” IBM在华能否得偿所愿
- 转 TCP中的序号和确认号
- 机器学习实战 | Python机器学习算法应用实践
- 暑假作业点评:运动会管理系统设计说明书
- 动态规划背包问题matlab,动态规划解决01背包问题
- 微信支付宝多商户解决方案
- 谈谈我的人才网站的的发展历程
- Echarts-----map(单独省级地图)
- 4星|《心流:最优体验心理学》:如何在工作生活中发现幸福:找到意义与目标并且专注其中...
- Web前端Table中的tr和td按百分比设定宽度和高度,当内容超出时Table变型解决
- 在进销存管理中经常提到红冲,那什么是红冲呢?
- 数据透视表与mysql_通过sql做数据透视表,数据库表行列转换(pivot和Unpivot用法)(一)...
- springboot+jsp电子元件仓库管理系统javaweb
- 敢达决战服务器维护,《敢达决战》维护结束!服务器开启again
- 劳特巴赫的基础使用(一)
热门文章
- python函数内部变量能改变外部吗_python中,如何利用函数修改外部变量,注意我的要求...
- C语言windows编程编写窗口
- 网站建设特定操作流程了解一下不吃亏
- java正则表达式逗号_正则表达式只匹配逗号而不是括号?
- 网络计算机统考报名网,2017年《计算机应用基础》统考试题及答案
- android mysql项目实例_android项目中单实例数据库类
- java版mc植物生长条件_植物生长需要的五个条件
- gp338信令_【对讲机的那点事】摩托罗拉GP338的MDC1200信令选呼如何编程?
- android BiConsumer 使用 ClassNotDefException
- 局部敏感哈希算法(Locality Sensitive Hashing)