1. Write a program to find the node at which the intersection of two singly linked lists begins.
  1. For example, the following two linked lists:
  1. A: a1 a2

  2. c1 c2 c3

  3. B: b1 b2 b3
  1. begin to intersect at node c1.
  2.  
  3. 题意:就是求取两个链表的交汇点,
    要求:无交汇点返回NULL,否则返回交汇点的地址。
  1.  
  1.  

  1.  
  2. 暴力思路:O(nm),固定A链表的第一个点,依次遍历B链表看是否有相同的点;接着固定A链表的第二个点,再依次遍历B链表,直到找到相同点为止。
    hash解:时间复杂度O(n+m),空间O(n)或者O(m)
    两指针解法:我们发现只要两个链表长度一样,就只需同时后移节点指针比较一个,若其中一个较长呢,其实处理一下,把两个链表变成一样长即可。
    解法步骤:
    1.求出两个链表的长度
    2.若一样长,无需处理;若其中一个较长,则只需让较长的链表先走abs(lengthA-lengthB)步即可。
    3.同时后移节点指针,直到找到交汇点。
    代码:
  1. class Solution {
  2. private:
  3. int getLength(ListNode* head){
  4. if(head==NULL) return ;
  5. ListNode* p=head;
  6. int res=;
  7. while (p->next!=NULL)
  8. {
  9. ++res;p=p->next;
  10. }
  11. return res;
  12. }
  13. public:
  14. ListNode *getIntersectionNode(ListNode *headA, ListNode *headB) {
  15. if(headA==NULL||headB==NULL) return NULL;
  16. int length_A=getLength(headA);
  17. int length_B=getLength(headB);
  18. ListNode* pA=headA;
  19. ListNode* pB=headB;
  20. int dis=;
  21. if (length_A>length_B)
  22. {
  23. dis=length_A-length_B;
  24. while (dis>)
  25. {
  26. --dis;
  27. pA=pA->next;
  28. }
  29. }else if(length_A<length_B)
  30. {
  31. dis=length_B-length_A;
  32. while (dis>)
  33. {
  34. --dis;
  35. pB=pB->next;
  36. }
  37. }
  38. while (pA!=NULL&&pB!=NULL&&pA!=pB)
  39. {
  40. pA=pA->next;
  41. pB=pB->next;
  42. }
  43. if(pA==pB&&pA!=NULL) return pA;
  44. else return NULL;
  45. }
  46. };
  1.  

Intersection of Two Linked Lists(链表)的更多相关文章

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

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

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

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

  4. 2016.5.24——Intersection of Two Linked Lists

    Intersection of Two Linked Lists 本题收获: 1.链表的输入输出 2.交叉链表:这个链表可以有交叉点,只要前一个节点的的->next相同即可. 题目:Inters ...

  5. LeetCode: Intersection of Two Linked Lists 解题报告

    Intersection of Two Linked Lists Write a program to find the node at which the intersection of two s ...

  6. LeetCode--LinkedList--160. Intersection of Two Linked Lists(Easy)

    160. Intersection of Two Linked Lists(Easy) 题目地址https://leetcode.com/problems/intersection-of-two-li ...

  7. 160. Intersection of Two Linked Lists【easy】

    160. Intersection of Two Linked Lists[easy] Write a program to find the node at which the intersecti ...

  8. LeetCode_160. Intersection of Two Linked Lists

    160. Intersection of Two Linked Lists Easy Write a program to find the node at which the intersectio ...

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

随机推荐

  1. java操作Excel、PDF文件

    java操作Excel.PDF文件 分享者:Vashon 分享来源:CSDN博客 下面这些是在开发中用到的一些东西,有的代码贴的不是完整的,只是贴出了关于操作EXCEL的代码: jxl是一个*国人写的 ...

  2. oracle适配器连接不上解决方案

    Oracle适配器连接不上解决方案 作者:Vashon oracle 的Developer连接不上报错:listener does not currently know of SID given in ...

  3. Windows Server 启用匿名共享

    1.开始 → 运行 → gpedit.msc,打开组策略编辑器: 2.依次展开"计算机配置" → "windows设置" → "安全设置"  ...

  4. saltstack 源码安装

    面向对象编程(oop) 面向对象: 面向对象三大特性: 封装 继承 多肽封装: 封装就是将具体的客观事物封装成抽象的类.并且类可以把自己的数据和方法只让可信的类或者对象操作,对不可行的进行信息隐藏继承 ...

  5. 阿里云人脸比对API封装

    这是根据封装是根据阿里云官方给的Demo进行修改的,当时是因为编写微信小程序云函数需要使用到阿里云人脸比对接口,才对其进行封装的. 记录下来,方便下次使用. 复制下来可以直接使用. 用到的依赖如下: ...

  6. atoi (String to Integer) leetcode

    将字符串转化为数字,其注意事项有: Requirements for atoi: The function first discards as many whitespace characters a ...

  7. postman使用--环境变量

    变量 postman提供了变量设置,有四种变量类型本地变量全局变量环境变量 数据变量 什么是环境变量 环境变量指在不同环境,同一个变量值随着环境不同而变化,比如在测试环境时,host为:dev.pos ...

  8. 洛谷——P4932 浏览器

    P4932 浏览器 __stdcall给了你n个点,第i个点有权值x[i],对于两个点u和v,如果x[u] xor x[v]的结果在二进制表示下有奇数个1,那么在u和v之间连接一个Edge,现在__s ...

  9. 整理几个牛人博客以及OJ

    Blogs 陈立杰(wjmzbmr):http://wjmzbmr.com/ 飘过的小牛:http://blog.csdn.net/niushuai666 王垠:http://www.yinwang. ...

  10. Linux离线安装redis集群

    一.应用场景介绍 本文主要是介绍Redis集群在Linux环境下的安装讲解,联网环境安装较为简单,这里只说脱机的Linux环境下是如何安装的.因为大多数时候,公司的生产环境是在内网环境下,无外网,服务 ...