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.

这个题目是要找出两个链表的交叉点,该如何解决呢?如果两个链表相交,我们从A链表出发移动一段距离alen,出B列表出发移动一段距离blen,那么会发现他们指向同一个节点c1。那么这个距离是多少呢?

我们把每一个链表看成两段,不相交的一段和相交的一段。相交的一段对于两个链表长度是一样的,不想交的一段链表的长度是不同的。如果我们分别计算出两个链表的长度,然后计算他们长度的差值f,然后在较长的链表上先移动距离f,再同时从A,B链表出发开始遍历,若发现他们指向同一个节点,则他们相交并返回相交的点,若他们不想交,则会遍历到链表的尾部,则返回null。代码如下:

 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     public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
14         ListNode p1 = headA, p2 = headB;
15         int len1 = 0, len2 = 0;
16         while (p1 != null) {    //求链表A的长度
17             p1 = p1.next;
18             len1++;
19         }
20         while (p2 != null) {    //求链表B的长度
21             p2 = p2.next;
22             len2++;
23         }
24         p1 = headA;
25         p2 = headB;
26         if (len1 > len2) {      //计算链表长度的差值并在较长的链表上向后移动|len1-len2|
27             for (int i = 0;i < len1 - len2; i++) {
28                 p1 = p1.next;
29             }
30         } else {
31             for (int i = 0;i < len2 - len1; i++) {
32                 p2 = p2.next;
33             }
34         }
35         while (p1 != p2) {      //向后遍历链表A和链表B,找到相交的节点,若遍历到最后,返回null
36             p1 = p1.next;
37             p2 = p2.next;
38         }
39         return p1;
40     }
41 }

转载于:https://www.cnblogs.com/liujinhong/p/5386115.html

LeetCode OJ 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 python3 简单题160. Intersection of Two Linked Lists

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

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

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

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

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

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

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

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

最新文章

  1. ISP【二】————camera ir图
  2. python2.7和3.7共存_centos7 python2 和python3 共存[转]
  3. 用Spring的BeanUtils前,建议你先了解这几个坑!
  4. spring中怎么访问MySQL过程_【FunnyBear的Java之旅 - Spring篇】7步连接MySQL
  5. 干货|全面解析知识图谱:一种帮助通向强AI的数据科学技术
  6. python有趣的小项目-有趣的python小项目,自动生成有趣的表情包!
  7. 60行代码俄罗斯方块
  8. debian架设php,Debian 下搭建php-fastcgi方式,nginx
  9. Django模版(三)
  10. php函数fiter,PHP filter_var() 函数和 Filter 函数使用说明
  11. java的for循环取出数据只是拿到最后一个_从MySQL中导数据到HBase
  12. mysql默认值是随机数_mysql生成指定位数的随机数及批量生成随机数的方法
  13. [Vue warn]: Attribute id is ignored on component div because the component is a fragment instanc
  14. Windows 不能在 本地计算机 启动 SQL Server(MSSQLSERVER) 10048
  15. 【Amaple教程】4. 组件
  16. Android常用对话框大全——Dialog
  17. jsdroid 教程_安卓10 ROM编译教程(六):清除编译与更新源码
  18. idea中项目文件颜色含义
  19. 【路径规划】基于改进差分算法实现三维多无人机协同航迹规划
  20. matlab epics,基于EPICS/MATLAB图像处理的光束位置测量系统

热门文章

  1. g2o求解BA 第10章
  2. 从多级延迟触发器到边沿检测
  3. jmeter监控服务资源
  4. webpack-dev-server 搭建本地服务以及浏览器实时刷新
  5. 使用默认Model Binding支持集合类
  6. oracle调整Lock_sga参数而不使用虚拟内存
  7. php跳转分站,PHP判断IP并转跳到相应城市分站的方法
  8. 蓝桥杯java能用编译器1吗_学java的你,这些英文单词都掌握了吗?
  9. mysql用 fifo 记录日志_MySQL一丢丢知识点的了解
  10. Oh-My-Zsh 中安装 NVM