题目

两个链表的交叉

请写一个程序,找到两个单链表最开始的交叉节点。

样例

下列两个链表:

  1. A: a1 a2

  2. c1 c2 c3

  3. B: b1 b2 b3

在节点 c1 开始交叉。

注意

  • 如果两个链表没有交叉,返回null
  • 在返回结果后,两个链表仍须保持原有的结构。
  • 可假定整个链表结构中没有循环。
挑战

需满足 O(n) 时间复杂度,且仅用 O(1) 内存。

解题

尝试用时间复杂度是O(NM),却没有解决,在这个博客看到根据两个链表的特性进行解决。

就如同上图,两个链表相交的部分一定在尾部的,如果两个链表尾部对齐,按照短的链表头节点开始,同时时对两个链表进行遍历,找到相同节点处就是共同的节点。

这里为了找到短链表的都节点在长链表处的位置<这里的位置是相对的,他们不一定是在一起的,这里只是为了让尾对齐>。先求两个链表的长度

假设长链表是A 长度lenA 短链表B 长度LenB

长链表头节点开始走,并lenA-=1 当lenA==lenB的时候说明链表尾部对齐了,这样就开始直接按顺序比较链表节点值是否相等了。时间复杂度是O(M+N)

  1. # Definition for singly-linked list.
  2. # class ListNode:
  3. # def __init__(self, x):
  4. # self.val = x
  5. # self.next = None
  6.  
  7. class Solution:
  8. # @param headA: the first list
  9. # @param headB: the second list
  10. # @return: a ListNode
  11. def getIntersectionNode(self, headA, headB):
  12. # Write your code here
  13. if headA == None:
  14. return None
  15. if headB == None:
  16. return None
  17. lenA = self.getLength(headA)
  18. lenB = self.getLength(headB)
  19. A = None
  20. B = None
  21. if lenA > lenB:
  22. A = headA
  23. B = headB
  24. else:
  25. A = headB
  26. B = headA
  27. tmp = lenA
  28. lenA = lenB
  29. lenB = tmp
  30. while lenA>lenB:
  31. lenA -=1
  32. A = A.next
  33. while A and B:
  34. if A.val == B.val:
  35. return A
  36. A = A.next
  37. B = B.next
  38.  
  39. def getLength(self,head):
  40. length = 0
  41. p = head
  42. while p!=None:
  43. p = p.next;
  44. length +=1
  45. return length

Python Code

总耗时: 340 ms.

  1. /**
  2. * Definition for singly-linked list.
  3. * public class ListNode {
  4. * int val;
  5. * ListNode next;
  6. * ListNode(int x) {
  7. * val = x;
  8. * next = null;
  9. * }
  10. * }
  11. */
  12. public class Solution {
  13. /**
  14. * @param headA: the first list
  15. * @param headB: the second list
  16. * @return: a ListNode
  17. */
  18. public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
  19. // Write your code here
  20. if(headA == null)
  21. return null;
  22. if(headB == null)
  23. return null;
  24. if(headA == headB )
  25. return headA;
  26. // 求 A B的长度
  27. int lenA = getLength(headA);
  28. int lenB = getLength(headB);
  29. ListNode A = null;
  30. ListNode B = null;
  31. // A是比较长的链表
  32. if(lenA>lenB){
  33. A = headA;
  34. B = headB;
  35. }else{
  36. A = headB;
  37. B = headA;
  38. int tmp = lenA;
  39. lenA = lenB;
  40. lenB = tmp;
  41. }
  42. while(lenA>lenB){
  43. A = A.next;
  44. lenA--;
  45. }
  46.  
  47. while(A!=null && B!=null){
  48. if(A.val == B.val){
  49. return A;
  50. }
  51. A = A.next;
  52. B = B.next;
  53. }
  54. return null;
  55. }
  56. public int getLength(ListNode head){
  57. int length = 0;
  58. ListNode p = head;
  59. while(p!=null){
  60. length++;
  61. p = p.next;
  62. }
  63. return length;
  64. }
  65. }

Java Code

总耗时: 2028 ms

lintcode 中等题:Intersection of Two Linked Lists 两个链表的交叉的更多相关文章

  1. intersection of two linked lists.(两个链表交叉的地方)

    Write a program to find the node at which the intersection of two singly linked lists begins. For ex ...

  2. 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 ...

  3. [LC]141题 Intersection of Two Linked Lists (相交链表)(链表)

    ①中文题目 编写一个程序,找到两个单链表相交的起始节点. 如下面的两个链表: 在节点 c1 开始相交. 注意: 如果两个链表没有交点,返回 null.在返回结果后,两个链表仍须保持原有的结构.可假定整 ...

  4. [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 ...

  5. LeetCode算法题-Intersection of Two Linked Lists(Java实现)

    这是悦乐书的第178次更新,第180篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第37题(顺位题号是160).编写程序以找到两个单链表交叉的节点.例如: 以下两个链表: ...

  6. [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 ...

  7. Intersection of Two Linked Lists两链表找重合节点

    Write a program to find the node at which the intersection of two singly linked lists begins. For ex ...

  8. [LintCode] Intersection of Two Linked Lists 求两个链表的交点

    Write a program to find the node at which the intersection of two singly linked lists begins. Notice ...

  9. [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 ...

随机推荐

  1. 【Qt】Qt之自定义界面(右下角冒泡)【转】

    简述 网页右下角上经常会出现一些提示性的信息,桌面软件中也比较常见,类似360新闻.QQ消息提示一样! 这种功能用动画实现起来很简单,这节我们暂时使用定时器来实现,后面章节会对动画框架进行详细讲解. ...

  2. Ubuntu16.04.1 安装MyCat

    Mycat是一个开源的分布式数据库系统,但是由于真正的数据库需要存储引擎,而Mycat并没有存储引擎,所以并不是完全意义的分布式数据库系统. 安装Java环境,配置全局环境变量 MyCAT是使用JAV ...

  3. 用 Function.apply() 的参数数组化来提高 JavaScript程序性能

    我们再来聊聊Function.apply() 在提升程序性能方面的技巧. 我们先从 Math.max() 函数说起, Math.max后面可以接任意个参数,最后返回所有参数中的最大值. 比如 aler ...

  4. mysql数据库表格导出为excel表格

    在本地数据库中操作如下: 由于excel表格的编码是GBK,所以导出时要加一个设置字符编码: select * from 某个表 into outfile 'd:/文件名.xls' CHARACTER ...

  5. Python线性时间排序——桶排序、基数排序与计数排序

    1. 桶排序 1.1 范围为1-M的桶排序 如果有一个数组A,包含N个整数,值从1到M,我们可以得到一种非常快速的排序,桶排序(bucket sort).留置一个数组S,里面含有M个桶,初始化为0.然 ...

  6. Eclipse 下 opennms 开发环境搭建

    1.eclipse3.5或更高版本,并且使用纯净的java版.下载地址:Eclipse for Java Developers. 2.安装需要的插件.通过Help/Install New Softwa ...

  7. z470 装黑苹果 10.92

    1.分两个区,一个是mac安装区,一个是镜像拷贝区. 2.把镜像压进去. 3.安装好系统. 4.把镜像区的 extent拷贝到安装好的系统盘里去. 5.安装驱动,网盘里有.还有系统也在网盘里. 6.声 ...

  8. python学习小结6:模块

    模块:在Python中有一个概念叫做模块(module),简单地说,模块就是一个保存了Python代码的文件.          模块能定义函数,类和变量,模块里也能包含可执行的代码.         ...

  9. eclipse 配置git ssh登录

    实现需要安装git的插件,由于我使用的adt和eclipse for javaee版本两个,都已经安装了git插件,就不再演示了,网上都有. 这篇文章主要是介绍使用ssh进行认证的方式. 1.首先,配 ...

  10. ffmpeg 打开视频流太慢(下)

    前面的博文中已经交代过,ffmpeg打开视频慢主要是因为av_find_stream_info 耗时久.下面给出重写查找音视频stream info的一段代码,用来替代av_find_stream_i ...