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 ...
随机推荐
- nginx开启网站目录浏览功能
一.开启全站目录浏览功能 编辑nginx.conf, 在http下面添加以下内容: autoindex on; # 开启目录文件列表 autoindex_exact_size on; # 显示出文件的 ...
- 初识程序设计,Linux使用问题记录
刚开始实在csdn写的,排版csdn更好看,本文链接:https://blog.csdn.net/qq_30282649/article/details/102910972 @[TOC](刚开始学习计 ...
- 强烈推荐一个和朋友远程桌面的软件teamviewer
强烈推荐一个和朋友远程桌面的软件teamviewer个人认为:贼溜
- git强制合并另一个分支
New分支和Old分支都修改了同样的部分,有冲突,但是想在Old分之上合并New分支的内容,并且以New分支为主,就是不自己手动解决冲突,碰到冲突,直接以New分支为主. 参考 https://git ...
- ECMAScript 初探 - 对象篇
一.对象 如果你用过 C++ 或 Java,肯定熟悉类(class).在 ECMAScript 中并没有 "类" 这个词, 其对应的是 "对象定义",不过这太拗 ...
- root账号无法上传文件到Linux服务器
普通权限的账号,通过ftp工具,可以正常连上Linux服务器,可以正常上传文件.但是root账号却无法上传文件. 网上搜了半天才知道,默认情况下vsftp是不允许root用户登录的,可以通过修改限制来 ...
- linux ,查看端口
netstat -antlp | grep java 注:grep java是过滤所有java进程
- main 函数返回值
[1]main函数 [2]main() 经典的C风格函数头,如下: main() 在C语言中,省略返回类型相当于说函数的类型为int. 但是,需要明确,C++逐步淘汰了这种用法. 另外,在C语言中,让 ...
- Linux下安装Python3.6.8并安装包
一.问题在Linux下面安装Python3.6.8,由于在Linux中的Python是2.7.x的版本因此,我们需要在Linux中新下载一个Python 二.解决1.python的安装(1)下载包利用 ...
- Java学习:Properties类
Java学习:Properties类 学习目标 认识properties文件,理解其含义,会正确创建properties文件. 会使用java.util.Properties类来操作propertie ...