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 ...
随机推荐
- 如何做到MySQL的高可用?
本课时的主题是“MySQL 高可用”,主要内容包含: 什么是高可用性 MySQL 如何提升 MTBF MySQL 如何降低 MTTR 避免单点失效 基础软硬件避免单点 MySQL 高可用架构选型 故障 ...
- SLAM:暑期学校
专门开个(大)坑: RGB-D SLAM:姜翰青,商汤 视觉SLAM:章国锋,浙大CAD&CG国家重点实验室
- Codeforces Round #609 (Div. 2) 题解
Equation Modulo Equality Long Beautiful Integer Domino for Young K Integers Equation \[ Time Limit: ...
- 初赛Part2
初赛 时间复杂度 主定理(必考) \[ T(n) = aT(\frac{n}{b})+f(n) \] 其中,\(n\)为问题的规模,\(a\)为递推下子问题的数量,\(\frac{n}{b}\)为每个 ...
- Philosopher(set 线段树合并)
直接维护乘积是肯定不可行的, 精度会爆炸, 于是我们来维护对数的和, 最后来计算最高位即可 那么转换成区间求和, 区间排序 区间排序的方式可以采用线段树维护最大递增块来解决,外层用set来维护线段树的 ...
- 【ECNU3386】Hunter's Apprentice(多边形有向面积)
点此看题面 大致题意: 按顺序给你一个多边形的全部顶点,让你判断是按顺时针还是逆时针给出的. 多边形有向面积 显然我们知道,叉积可以求出两个向量之间有向面积的两倍. 所以,我们三角剖分,就可以求出四边 ...
- inputType导致TextView不能多行显示
今天遇到一个问题很纳闷,那就是TextView不能自动换行多行显示,因为我的印象是TextView默认是可以自动换行多行显示的,今儿个怎么就不行呢. 最终找到原因,是因为设置了inputType属性导 ...
- HTML连载27-层叠性&优先级&!important用法
一.层叠性 1.定义:CSS处理冲突的一种能力 2.注意点:层叠性只有在多个选择器中“同一标签”,然后又设置了“相同的属性”,才会发生层叠性 3.CSS缩写:Cascading StyleSheet ...
- Docker 安装 Request Tracker 工单系统
1.需求 docker 安装工单系统 Request Tracker,并需要支持 LDAP 登入. 2.制作镜像 1)request-tracker-base镜像 第一个镜像安装一些基础支持软件,如 ...
- vue + element 文件上传 并将文件转 base64
当时有一个需求 是需要用到上传文件这个功能,并且需要将文件转为 base64 给到后台.网上找的全是啥图片转base64 肯定是因为这类需求比较常见.当时有点懵了.后面一想,都他娘是文件啊.然后就找到 ...