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。代码如下:

 /**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) {
* val = x;
* next = null;
* }
* }
*/
public class Solution {
public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
ListNode p1 = headA, p2 = headB;
int len1 = 0, len2 = 0;
while (p1 != null) { //求链表A的长度
p1 = p1.next;
len1++;
}
while (p2 != null) { //求链表B的长度
p2 = p2.next;
len2++;
}
p1 = headA;
p2 = headB;
if (len1 > len2) { //计算链表长度的差值并在较长的链表上向后移动|len1-len2|
for (int i = 0;i < len1 - len2; i++) {
p1 = p1.next;
}
} else {
for (int i = 0;i < len2 - len1; i++) {
p2 = p2.next;
}
}
while (p1 != p2) { //向后遍历链表A和链表B,找到相交的节点,若遍历到最后,返回null
p1 = p1.next;
p2 = p2.next;
}
return p1;
}
}

LeetCode OJ 160. Intersection of Two Linked Lists的更多相关文章

  1. 【一天一道LeetCode】#160. Intersection of Two Linked Lists

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Write a ...

  2. 【LeetCode】160. Intersection of Two Linked Lists 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 双指针 栈 日期 题目地址:https://leet ...

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

  4. LeetCode OJ: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. 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 ...

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

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

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

  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. 配置 Apache 的虚拟主机

    1.在host配置比如: 找到记事本以管理员的身份打开,然后文件->打开  C:\Windows\System32\drivers\etc    下面的hosts文件 127.0.0.1 www ...

  2. Android中使用OKHttp上传图片,从相机和相册中获取图片并剪切

    效果:注意:1:网络权限<;;;); intent.putExtra(); ); intent.putExtra(); intent.putExtra(, byteArrayOutputStre ...

  3. HDU Herding

    F - Herding Time Limit:1000MS       Memory Limit:32768KB      64bit IO Format:%I64d & %I64u Subm ...

  4. 修改MANIFEST.MF方法

    步骤: 1.用winrar等其他解压工具,右键选择"用winrar打开": 2.找到MANIFEST.MF文件,鼠标左键拖拽到桌面: 3.最后一行加入Main-Class:(空格) ...

  5. NameCheap域名注册商的几个特点介绍

    作为站长,我们拥有选择3-5家域名注册商,把自己的域名放置在不同的平台也是应该的,因为不同的注册商都有不同的值得我们选择的理由.作为已经使用namecheap注册商超过5年,拥有数百个域名的我来说,分 ...

  6. 云锁Linux服务器安全软件安装及防护webshell、CC、XSS跨站攻击设置

    无论我们在使用电脑,还是使用VPS/服务器的时候,最为担心的就是服务器是否有安全问题,尤其是网站服务器再遭受攻击的时候如何得到防护.对于大 部分站长用户来说,我们可能只会使用基础的环境,如果真遇到问题 ...

  7. SpringMVC 学习-拦截器 HandlerInterceptor 类

    一.拦截器 HandlerInterceptor 类的作用 SpringMVC 的拦截器类似于 Servlet 开发中的过滤器 Filter,用于对处理器进行预处理和后处理. 二.怎么使用呢? 1. ...

  8. js中checkbox反选

    <!DOCTYPE HTML> <html> <head> <meta http-equiv="Content-Type" content ...

  9. Commix命令注入漏洞利用

    介绍 项目地址:https://github.com/stasinopoulos/commix Commix是一个使用Python开发的漏洞测试工具,这个工具是为了方便的检测一个请求是否存在命令注入漏 ...

  10. Struts2权威指南笔记

    Struts2权威指南笔记 1.mvc特点包括: ① 多个视图可以对应一个模型 ② 模型返回的数据与显示逻辑分离 ③ 应用层被分隔为三层,降低了各层之间的耦合,提供了应用的可扩展性 ④ 控制层的概念也 ...