①中文题目

编写一个程序,找到两个单链表相交的起始节点。

如下面的两个链表:

在节点 c1 开始相交。

注意:

如果两个链表没有交点,返回 null.
在返回结果后,两个链表仍须保持原有的结构。
可假定整个链表结构中没有循环。
程序尽量满足 O(n) 时间复杂度,且仅用 O(1) 内存。

②思路

遍历,O(M*N)的遍历,直到找出相等的结点(不是val相等)

③我的代码(很慢)

 public class Solution {
public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
ListNode temp1=headA;
ListNode temp2=headB;
if(temp1==null||temp2==null)
return null;
while(temp1!=null){
if(temp2==null){ //当temp2遍历到尾巴的时候,
temp1=temp1.next; //temp1后移1位
temp2=headB; //temp2回到自己原本的头部,再来一遍
}
if(temp1==temp2)
return temp1;
temp2=temp2.next; //这一次比较,没找到相等的,于是temp2后移一步
}
return null;
}
}

④运行结果

执行结果:通过 。显示详情
执行用时 :674 ms, 在所有 Java 提交中击败了5.01%的用户
内存消耗 :48.5 MB, 在所有 Java 提交中击败了5.05%的用户

⑤别人的快速代码

 public class Solution {
public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
if (headA == null || headB == null) return null;
ListNode pA = headA, pB = headB;
while (pA != pB) {
pA = pA == null ? headB : pA.next;
pB = pB == null ? headA : pB.next;
}
return pA;
}
}

他的运行结果:

执行结果:

通过
显示详情
执行用时 :1 ms, 在所有 Java 提交中击败了100.00%的用户
内存消耗 :37.5 MB, 在所有 Java 提交中击败了85.92%的用户
 
他的思路:https://leetcode-cn.com/problems/intersection-of-two-linked-lists/solution/tu-jie-xiang-jiao-lian-biao-by-user7208t/。太厉害了。

⑥学到的知识

1、我自己的代码里,12行,不是val元素相等,而是要判断结点是否相等(结点包括元素和指针)

2、看看别人的代码里的思路,太厉害了。还给出了图解!

[LC]141题 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. 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. 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. LeetCode算法题-Intersection of Two Linked Lists(Java实现)

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

  5. lintcode 中等题:Intersection of Two Linked Lists 两个链表的交叉

    题目 两个链表的交叉 请写一个程序,找到两个单链表最开始的交叉节点. 样例 下列两个链表: A: a1 → a2 ↘ c1 → c2 → c3 ↗ B: b1 → b2 → b3 在节点 c1 开始交 ...

  6. 【LeetCode】Intersection of Two Linked Lists(相交链表)

    这道题是LeetCode里的第160道题. 题目讲的: 编写一个程序,找到两个单链表相交的起始节点. 如下面的两个链表: 在节点 c1 开始相交. 示例 1: 输入:intersectVal = 8, ...

  7. 160 Intersection of Two Linked Lists 相交链表

    编写一个程序,找到两个单链表相交的起始节点.例如,下面的两个链表:A:           a1 → a2                            ↘                   ...

  8. [LeetCode]160. Intersection of Two Linked Lists判断交叉链表的交点

    方法要记住,和判断是不是交叉链表不一样 方法是将两条链表的路径合并,两个指针分别从a和b走不同路线会在交点处相遇 public ListNode getIntersectionNode(ListNod ...

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

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

随机推荐

  1. nishang的介绍与使用

    0x01前言 Nishang是一个PowerShell攻击框架,它是PowerShell攻击脚本和有效载荷的一个集合.Nishang被广泛应用于渗透测试的各个阶段,本文主要介绍如何使用Nishang的 ...

  2. linux系统取证

    目录 0x00 查看系统信息 0x01 用户及组信息 0x02 防火墙及路由信息 0x03 查看网络.端口信息 0x04 系统运行信息查看 0x05 日志查看分析 0x00 查看系统信息 name-a ...

  3. [JZOJ5772]【NOIP2008模拟】今天你AK了吗?

    Description AK:All kill“你为什么没背书?”“没有为什么,我就是没背书.”“……我去年买了个表,G—U—N!”头铁王InFleaKing把背书的时间都拿去列排列了......n= ...

  4. Mac系统 安装Photoshop CC 2018破解版

    应用场景 本人从事前端行业,但是工作中有时也需要会点PS技能,之前一直使用window系统,突然换了Mac其他软件基本都差不多安装完了,就剩下比较难搞的PS.刚开始按照网上乱七八槽的教程下载过好多次都 ...

  5. python-url中中文编码与解码

    接口测试中遇到这种情况:get请求的传参有中文,以致url中有中文编码. 下面是常见的一种编码解码方式: from urllib.request import quote, unquote url = ...

  6. 2019.10.28 CSP%您赛第四场t3

    我写不动前两个了. 原谅一下. ____________________________________________________________________________________ ...

  7. 个人考场VIM配置

    前言 这个是我个人使用的Vim配置.双引号杠掉的部分是关于光标行列高亮(觉得难受而杠)和输入左括号同时打上右括号的(不习惯),如果要启用的话去掉引号即可. 将以下要启用的输入到$“./vimrc”$中 ...

  8. 自己动手破解Z.EntityFramework.Extensions 4.0.11.0的方法

    因为项目中使用到Z.EntityFramework.Extensions 和 Z.EntityFramework.Plus(免费开源)两个类库,但是Z.EntityFramework.Extensio ...

  9. JAVA必知必问问题-1

    数据类型 1) 基本类型: byte, int, long, float, double, boolean.... 要求记住基本类型占多少字节.范围.例如:byte 1字节范围-128-127,sho ...

  10. SQL查询选修了所有课程的学生姓名

    select sname from student where not exists (select * from course where not exists   (select * from s ...