160--Intersection Of Two Linked List
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的更多相关文章
- 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 ...
- [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 ...
- [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 ...
- 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 ...
- 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 ...
- 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 ...
- ✡ 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 ...
- 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. ...
- 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 ...
- 【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 ...
随机推荐
- Django项目中出现的错误及解决办法(ValueError: Dependency on app with no migrations: customuser)
写项目的时候遇到了类似的问题,其实就是没有生成迁移文件,执行一下数据库迁移命令就好了 ValueError: Dependency on app with no migrations: customu ...
- LG5201 「USACO2019JAN」Shortcut 最短路树
\(\mathrm{Shortcut}\) 问题描述 LG5201 题解 最短路树. 显然奶牛的路径就是从\(1\)走到各个草地,于是从\(1\)跑最短路,构建最短路树. 为了保证字典序,从\(1\) ...
- 几个简单js,普通写法和高逼格写法比较
1. 取数组中的数据,并对空值设置默认值: 常规写法: let arr = [0, 1, null, '', 'abc']; let newArr = []; arr.forEach((item, i ...
- <LinkedList> 160
160. Intersection of Two Linked Lists 分别从AB循环两次.如果第一次没循环到,第二次就会在节点相遇. public class Solution { public ...
- 11/2 下午 <String>
344. Reverse String 解法一(暴力法): 直接从两头往中间走,同时交换两边的字符即可 首位对调位置. class Solution { public void reverseStri ...
- Graphviz学习
(入门教程)[https://www.luogu.com.cn/blog/umr/graphviz]
- php和jquery生成QR Code
php生产QR Code 下载qrcode源码,地址:https://sourceforge.net/projects/phpqrcode/files/releases/ 1.解压后引入qrlib.p ...
- [转载]3.12 UiPath存在元素Element Exists的介绍和使用
一.Element Exists的介绍 使您能够验证UI元素是否存在,即使它不可见,输出的是一个布尔值 二.Element Exists在UiPath中的使用 1.打开设计器,在设计库中新建一个Seq ...
- $ is not defined与SpringMVC访问静态资源
编写前台Jquery代码时,遇到谷歌浏览器报错:Uncaught ReferenceError: $ is not defined 意味着Jquery库并没有导入到页面.这是有几种情况需要考虑: 1. ...
- 《Interest Rate Risk Modeling》阅读笔记——第三章:拟合期限结构
目录 第三章:拟合期限结构 思维导图 扩展 第三章:拟合期限结构 思维导图 扩展 NS 模型的变种