public class IntersectionOfTwoLinkedList {
/*
解法一:暴力遍历求交点。
时间复杂度:O(m*n) 空间复杂度:O(1)
*/
public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
if(headA==null||headB==null)
return null;
if (headA==headB)
return headA;
ListNode tempA=headA;
ListNode tempB=headB;
while (tempA!=null){
tempB=headB;
while (tempB!=null){
if (tempA==tempB)
return tempA;
tempB=tempB.next;
}
tempA=tempA.next;
}
return null;
}
/*
解法二:哈希表求解,思想和解法一差不多,将B存入哈希表,遍历A的节点看是否存在于B
时间复杂度:O(m+n) 空间复杂度:O(m)或O(n)
*/
public ListNode getIntersectionNode2(ListNode headA, ListNode headB) {
if(headA==null||headB==null)
return null;
if (headA==headB)
return headA;
Set<ListNode> set=new HashSet<>();
ListNode tempB=headB;
while (tempB!=null){
set.add(tempB);
tempB= tempB.next;
}
ListNode tempA=headA;
while (tempA!=null){
if (set.contains(tempA))
return tempA;
tempA= tempA.next;
}
return null;
}
/*
解法三:双指针:当两个链表长度相等时,只需要依次移动双指针,当指针指向的节点相同时,则有交点。
但是问题就在于,两个链表的长度不一定相等,所以就要解决它们的长度差。
一个字概述这个解法:骚。
*/
public ListNode getIntersectionNode3(ListNode headA, ListNode headB) {
if (headA==null||headB==null)
return null;
ListNode pA=headA;
ListNode pB=headB;
while (pA!=pB){
pA=pA==null?headB:pA.next;
pB=pB==null?headA:pB.next;
}
return pA;
}
}

160--Intersection Of Two Linked List的更多相关文章

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

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

  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. Java for 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. 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 求两个链表的起始重复位置 --------- java

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

  8. Java [Leetcode 160]Intersection of Two Linked Lists

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

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

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

随机推荐

  1. TensorFlow.js入门:一维向量的学习

    转载自:https://blog.csdn.net/weixin_34061042/article/details/89700664 一维向量及其运算 tensor 是 TensorFlow.js 的 ...

  2. Spring Cloud微服务安全实战_4-2_常见的微服务安全整体架构

    这个图适用于中小公司的微服务架构 微服务:SpringBoot 写的Rest服务 服务注册与发现:微服务所必备的.每个微服务都会到上边去注册.不管是微服务之间的调用,还是服务网关到微服务的转发,都是通 ...

  3. ASP.NET开发实战——(五)ASP.NET MVC & 分层

    上一篇文章简要说明了MVC所代表的含义并提供了详细的项目及其控制器.视图等内容的创建步骤,最终完成了一个简单ASP.NET MVC程序. 注:MVC与ASP.NET MVC不相等,MVC是一种开发模式 ...

  4. 洛谷P3232[HNOI2013]游走

    有一个无向简单连通图,顶点从 \(1\) 编号到 \(n\),边从 \(1\) 编号到 \(m\) 小Z在该图上进行随机游走,初始时小Z在\(1\)号顶点,每一步小Z以相等的概率随机选 择当前顶点的某 ...

  5. 清北学堂(2019 5 3) part 6

    今天讲STL 1.pair——<algorithm> 声明形如pair<int,int> x;(不是int也可以),表示x有前后两个成员,都是int类型,调用时写x.first ...

  6. 测开面试 | Python语言常见问题

    1.面向对象的概念? 面向对象编程,简称OOP,是一种程序设计思想 主要包括:类.继承.多态(子类方法覆盖父类方法).实例.属性.方法 2.什么是进程.线程.协程? 进程:独立数据空间,进程间不共享数 ...

  7. [LeetCode] 871. Minimum Number of Refueling Stops 最少的加油站个数

    A car travels from a starting position to a destination which is target miles east of the starting p ...

  8. [LeetCode] 415. Add Strings 字符串相加

    Given two non-negative numbers num1 and num2 represented as string, return the sum of num1 and num2. ...

  9. c语言编译器一个不会报错的陷阱

    1, 由于数字1和小写字母L(l)长得特别像,特别是VS默认字体里的,所以 double a; scanf("%1f",&a); double b; scanf(" ...

  10. Linux设置定时任务方法

    linux下定时执行任务的方法: 在LINUX中你应该先输入crontab -e,然后就会有个vi编辑界面,再输入0 3 * * 1 /clearigame2内容到里面 :wq 保存退出.   在LI ...