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. Python前言之Markdown使用

    一.Markdown基本语法 1.1标题 代码: # 一级标题 ## 二级标题 ### 三级标题 #### 四级标题 ##### 五级标题 ###### 六级标题 效果: 一级标题 二级标题 三级标题 ...

  2. js replace方法第二个参数,远不止你想的那么强大

    js replace() 方法,想必大家都不陌生. 定义和用法: replace()方法用于在字符串中用一些字符替换另一些字符,或者替换一个与正则表达式匹配的子串. stringObject.repl ...

  3. Arduino SPI驱动7引脚0.96寸OLED SSD1306 调试笔记

    https://www.geek-workshop.com/thread-37818-1-1.html 2.下载最新库https://learn.adafruit.com/monoc ... ibra ...

  4. SQL Server 迁移数据库 (二)分离和附加

    分离和附加其实比导入和导出,步骤要少一些,但是数据量大的话,跨服务器拷贝数据文件可能要慢一些 1. 分离数据库 这里最好选择断开链接,断开之前要确保你记得数据库的路径,一般默认都是C:\Program ...

  5. 数据结构——栈与递归(recursion)

    /* recursion.c */ /* 递归 */ #include <stdio.h> void interface(void); /* 斐波那契数列以及阶乘函数声明 */ long ...

  6. machine_math2

    1. 2. 3.拉格朗日对偶??? 弱对偶 强对偶: <1>slater条件(强对偶的充分条件): 1.凸函数. 2.存在一个可行解满足不等式成立. 4.KKT条件

  7. luogu P2221 [HAOI2012]高速公路题解

    题面 很套路的拆式子然后线段树上维护区间和的题.一般都是把式子拆成区间内几个形如\(\sum i*a_i, \sum i^2 * a_i\)的式子相加减的形式. 考虑一次询问[l,r]的答案怎么算: ...

  8. python字符串各种颜色输出

    \033[1;31;40m      # 1是显示方式(可选),31是字体颜色,40m 是字体背景颜色: \033[0m           # 恢复终端默认颜色,即取消颜色设置: #!/usr/bi ...

  9. Qt对话框之二:模态、非模态、半模态对话框

    一.模态对话框 模态对话框:阻塞同一应用程序中其它可视窗口输入的对话框.模态对话框有自己的事件循环,用户必须完成这个对话框中的交互操作,并且关闭了它之后才能访问应用程序中的其它任何窗口. 显示模态对话 ...

  10. springboot 远程调试

    首先以调试模式启动编译好的jar包,监听端口为5005 java -jar -Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=n,addre ...