lintcode 中等题:Intersection of Two Linked Lists 两个链表的交叉
题目
两个链表的交叉
请写一个程序,找到两个单链表最开始的交叉节点。
下列两个链表:
A: a1 → a2
↘
c1 → c2 → c3
↗
B: b1 → b2 → b3
在节点 c1 开始交叉。
- 如果两个链表没有交叉,返回
null
。 - 在返回结果后,两个链表仍须保持原有的结构。
- 可假定整个链表结构中没有循环。
需满足 O(n) 时间复杂度,且仅用 O(1) 内存。
解题
尝试用时间复杂度是O(NM),却没有解决,在这个博客看到根据两个链表的特性进行解决。
就如同上图,两个链表相交的部分一定在尾部的,如果两个链表尾部对齐,按照短的链表头节点开始,同时时对两个链表进行遍历,找到相同节点处就是共同的节点。
这里为了找到短链表的都节点在长链表处的位置<这里的位置是相对的,他们不一定是在一起的,这里只是为了让尾对齐>。先求两个链表的长度
假设长链表是A 长度lenA 短链表B 长度LenB
长链表头节点开始走,并lenA-=1 当lenA==lenB的时候说明链表尾部对齐了,这样就开始直接按顺序比较链表节点值是否相等了。时间复杂度是O(M+N)
- # Definition for singly-linked list.
- # class ListNode:
- # def __init__(self, x):
- # self.val = x
- # self.next = None
- class Solution:
- # @param headA: the first list
- # @param headB: the second list
- # @return: a ListNode
- def getIntersectionNode(self, headA, headB):
- # Write your code here
- if headA == None:
- return None
- if headB == None:
- return None
- lenA = self.getLength(headA)
- lenB = self.getLength(headB)
- A = None
- B = None
- if lenA > lenB:
- A = headA
- B = headB
- else:
- A = headB
- B = headA
- tmp = lenA
- lenA = lenB
- lenB = tmp
- while lenA>lenB:
- lenA -=1
- A = A.next
- while A and B:
- if A.val == B.val:
- return A
- A = A.next
- B = B.next
- def getLength(self,head):
- length = 0
- p = head
- while p!=None:
- p = p.next;
- length +=1
- return length
Python Code
总耗时: 340 ms.
- /**
- * Definition for singly-linked list.
- * public class ListNode {
- * int val;
- * ListNode next;
- * ListNode(int x) {
- * val = x;
- * next = null;
- * }
- * }
- */
- public class Solution {
- /**
- * @param headA: the first list
- * @param headB: the second list
- * @return: a ListNode
- */
- public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
- // Write your code here
- if(headA == null)
- return null;
- if(headB == null)
- return null;
- if(headA == headB )
- return headA;
- // 求 A B的长度
- int lenA = getLength(headA);
- int lenB = getLength(headB);
- ListNode A = null;
- ListNode B = null;
- // A是比较长的链表
- if(lenA>lenB){
- A = headA;
- B = headB;
- }else{
- A = headB;
- B = headA;
- int tmp = lenA;
- lenA = lenB;
- lenB = tmp;
- }
- while(lenA>lenB){
- A = A.next;
- lenA--;
- }
- while(A!=null && B!=null){
- if(A.val == B.val){
- return A;
- }
- A = A.next;
- B = B.next;
- }
- return null;
- }
- public int getLength(ListNode head){
- int length = 0;
- ListNode p = head;
- while(p!=null){
- length++;
- p = p.next;
- }
- return length;
- }
- }
Java Code
总耗时: 2028 ms
lintcode 中等题:Intersection of Two Linked Lists 两个链表的交叉的更多相关文章
- 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 ...
- [LC]141题 Intersection of Two Linked Lists (相交链表)(链表)
①中文题目 编写一个程序,找到两个单链表相交的起始节点. 如下面的两个链表: 在节点 c1 开始相交. 注意: 如果两个链表没有交点,返回 null.在返回结果后,两个链表仍须保持原有的结构.可假定整 ...
- [LeetCode]160.Intersection of Two Linked Lists(2个链表的公共节点)
Intersection of Two Linked Lists Write a program to find the node at which the intersection of two s ...
- LeetCode算法题-Intersection of Two Linked Lists(Java实现)
这是悦乐书的第178次更新,第180篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第37题(顺位题号是160).编写程序以找到两个单链表交叉的节点.例如: 以下两个链表: ...
- [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 ...
- Intersection of Two Linked Lists两链表找重合节点
Write a program to find the node at which the intersection of two singly linked lists begins. For ex ...
- [LintCode] Intersection of Two Linked Lists 求两个链表的交点
Write a program to find the node at which the intersection of two singly linked lists begins. Notice ...
- [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 ...
随机推荐
- 【Qt】Qt之自定义界面(右下角冒泡)【转】
简述 网页右下角上经常会出现一些提示性的信息,桌面软件中也比较常见,类似360新闻.QQ消息提示一样! 这种功能用动画实现起来很简单,这节我们暂时使用定时器来实现,后面章节会对动画框架进行详细讲解. ...
- Ubuntu16.04.1 安装MyCat
Mycat是一个开源的分布式数据库系统,但是由于真正的数据库需要存储引擎,而Mycat并没有存储引擎,所以并不是完全意义的分布式数据库系统. 安装Java环境,配置全局环境变量 MyCAT是使用JAV ...
- 用 Function.apply() 的参数数组化来提高 JavaScript程序性能
我们再来聊聊Function.apply() 在提升程序性能方面的技巧. 我们先从 Math.max() 函数说起, Math.max后面可以接任意个参数,最后返回所有参数中的最大值. 比如 aler ...
- mysql数据库表格导出为excel表格
在本地数据库中操作如下: 由于excel表格的编码是GBK,所以导出时要加一个设置字符编码: select * from 某个表 into outfile 'd:/文件名.xls' CHARACTER ...
- Python线性时间排序——桶排序、基数排序与计数排序
1. 桶排序 1.1 范围为1-M的桶排序 如果有一个数组A,包含N个整数,值从1到M,我们可以得到一种非常快速的排序,桶排序(bucket sort).留置一个数组S,里面含有M个桶,初始化为0.然 ...
- Eclipse 下 opennms 开发环境搭建
1.eclipse3.5或更高版本,并且使用纯净的java版.下载地址:Eclipse for Java Developers. 2.安装需要的插件.通过Help/Install New Softwa ...
- z470 装黑苹果 10.92
1.分两个区,一个是mac安装区,一个是镜像拷贝区. 2.把镜像压进去. 3.安装好系统. 4.把镜像区的 extent拷贝到安装好的系统盘里去. 5.安装驱动,网盘里有.还有系统也在网盘里. 6.声 ...
- python学习小结6:模块
模块:在Python中有一个概念叫做模块(module),简单地说,模块就是一个保存了Python代码的文件. 模块能定义函数,类和变量,模块里也能包含可执行的代码. ...
- eclipse 配置git ssh登录
实现需要安装git的插件,由于我使用的adt和eclipse for javaee版本两个,都已经安装了git插件,就不再演示了,网上都有. 这篇文章主要是介绍使用ssh进行认证的方式. 1.首先,配 ...
- ffmpeg 打开视频流太慢(下)
前面的博文中已经交代过,ffmpeg打开视频慢主要是因为av_find_stream_info 耗时久.下面给出重写查找音视频stream info的一段代码,用来替代av_find_stream_i ...