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

For example, the following two linked lists:

A:          a1 → a2↘c1 → c2 → c3↗
B:     b1 → b2 → b3

begin to intersect at node c1.

Notes:

  • If the two linked lists have no intersection at all, return null.
  • The linked lists must retain their original structure after the function returns.
  • You may assume there are no cycles anywhere in the entire linked structure.
  • Your code should preferably run in O(n) time and use only O(1) memory.
    找到相交的结点,很简单的题目,用双指针记录两个链表,如果相等则返回结点,如果没有则一直next下去,尽头没有next了就返回空。当其中一个指针走完的时候,就重定向到另外一个链表,这样子就可以让两个指针步数一致。在第二次迭代同时碰到交叉点结点。最后的时间复杂度是两个链表的长度之和,考虑最坏情况O(m+n)。例:A={1,2,3,4,5},B={6,7,8,3,4,5}。A的长度比B小,A先走完了,此时B走到4的位置,A重定向后在链表B的6的位置,此时B走到5,然后B重定向到A链表的位置1,最后两个指针距离交叉点3的步数一致。

     1 /**
     2  * Definition for singly-linked list.
     3  * struct ListNode {
     4  *     int val;
     5  *     struct ListNode *next;
     6  * };
     7  */
     8 struct ListNode *getIntersectionNode(struct ListNode *headA, struct ListNode *headB) {
     9     if(!headA || !headB)    return NULL;
    10     struct ListNode *pa=headA,*pb=headB;
    11     while(1){
    12         if(pa==pb)
    13             return pa;
    14         else if(!pa->next && !pb->next)
    15             return NULL;
    16         else{
    17             if(pa->next)
    18                 pa=pa->next;
    19             else
    20                 pa=headB;
    21             if(pb->next)
    22                 pb=pb->next;
    23             else
    24                 pb=headA;
    25         }
    26     }
    27 }

    看到别的大神更简洁的写法,同样的原理:

     1 /**
     2  * Definition for singly-linked list.
     3  * struct ListNode {
     4  *     int val;
     5  *     struct ListNode *next;
     6  * };
     7  */
     8 struct ListNode *getIntersectionNode(struct ListNode *headA, struct ListNode *headB) {
     9     if(!headA || !headB)    return NULL;
    10     struct ListNode *pa=headA,*pb=headB;
    11     while(pa!=pb){
    12         pa=pa==NULL?headB:pa->next;
    13         pb=pb==NULL?headA:pb->next;
    14     }
    15     return pa;
    16 }

转载于:https://www.cnblogs.com/real1587/p/9886734.html

160. Intersection of Two Linked Lists相关推荐

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

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

  3. 【LeetCode从零单排】No.160 Intersection of Two Linked Lists

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

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

  5. letecode [160] - Intersection of Two Linked Lists

    Write a program to find the node at which the intersection of two singly linked lists begins. 题目大意: ...

  6. LeetCode OJ 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 ...

  7. LeetCode 160. Intersection of Two Linked Lists

    题目: Write a program to find the node at which the intersection of two singly linked lists begins. Fo ...

  8. 160. Intersection of Two Linked Lists(剑指Offer-两个链表的第一个公共结点)

    题目: Write a program to find the node at which the intersection of two singly linked lists begins. Fo ...

  9. leetcode python3 简单题160. Intersection of Two Linked Lists

    1.编辑器 我使用的是win10+vscode+leetcode+python3 环境配置参见我的博客: 链接 2.第一百六十题 (1)题目 英文: Write a program to find t ...

最新文章

  1. Python Django HttpRequest请求对象常见属性和方法
  2. Ribbon-2通过代码自定义配置ribbon
  3. 阿里P8面试官都说太详细了,面试资料分享
  4. 一则 gfs2 集群文件系统无法挂载的解决案例
  5. ServletContext的应用
  6. cnblogs用户体验评价
  7. 阶段1 语言基础+高级_1-3-Java语言高级_06-File类与IO流_07 缓冲流_1_缓冲流的原理...
  8. HDFS的shell命令
  9. 使用 bioMart 包获取数据库信息
  10. Keil编译后——代码长度超过版本限制
  11. 产品经理应该多读哪些书?
  12. html span向上对齐,css也能让span靠左布局
  13. linux内核配置cpu相关,Linux内核配置
  14. 我的职场十年:谈谈普通员工的各种低级错误
  15. 西工大c语言noj作业答案,西工大noj答案
  16. 苹果ppt_如何选择一款趁手的PPT软件 | 一千零一夜PPT系列
  17. 华为Huawei服务器IBMC默认用户密码
  18. vmware workstation 9 安装Ubuntu 12.04 WIN7 64 位 出现问题及解决办法
  19. 怎样读 nacos 源码——服务端处理客户端注册解读
  20. 尚硅谷--Java--基础篇(717集)

热门文章

  1. maven学习笔记之IDEA+Maven+Jetty运行一个简单的web项目
  2. 复制字符串小程序笔记
  3. 探索7.x, 全面解析Activity启动框架 (2)
  4. JZOJ 5439. 【NOIP2017提高A组集训10.31】Calculate
  5. r语言 c 函数返回值,R语言入门 输出函数 cat、print、paste等区别理解
  6. sqlserver 储存过程 批量更新_大白菜怎么储存过冬,好吃还不烂?掌握方法,其实很简单...
  7. LPS25HB 寄存器读写程序解读
  8. cnc加工中心保养表_CNC加工中心有哪些日常保养方法?
  9. python回归模型 变量筛选_如何进行高维变量筛选和特征选择(一)?Lasso回归
  10. 条件随机场(CRF)相对于HMM,MEMM的优势