1. public class IntersectionOfTwoLinkedList {
  2. /*
  3. 解法一:暴力遍历求交点。
  4. 时间复杂度:O(m*n) 空间复杂度:O(1)
  5. */
  6. public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
  7. if(headA==null||headB==null)
  8. return null;
  9. if (headA==headB)
  10. return headA;
  11. ListNode tempA=headA;
  12. ListNode tempB=headB;
  13. while (tempA!=null){
  14. tempB=headB;
  15. while (tempB!=null){
  16. if (tempA==tempB)
  17. return tempA;
  18. tempB=tempB.next;
  19. }
  20. tempA=tempA.next;
  21. }
  22. return null;
  23. }
  24. /*
  25. 解法二:哈希表求解,思想和解法一差不多,将B存入哈希表,遍历A的节点看是否存在于B
  26. 时间复杂度:O(m+n) 空间复杂度:O(m)或O(n)
  27. */
  28. public ListNode getIntersectionNode2(ListNode headA, ListNode headB) {
  29. if(headA==null||headB==null)
  30. return null;
  31. if (headA==headB)
  32. return headA;
  33. Set<ListNode> set=new HashSet<>();
  34. ListNode tempB=headB;
  35. while (tempB!=null){
  36. set.add(tempB);
  37. tempB= tempB.next;
  38. }
  39. ListNode tempA=headA;
  40. while (tempA!=null){
  41. if (set.contains(tempA))
  42. return tempA;
  43. tempA= tempA.next;
  44. }
  45. return null;
  46. }
  47. /*
  48. 解法三:双指针:当两个链表长度相等时,只需要依次移动双指针,当指针指向的节点相同时,则有交点。
  49. 但是问题就在于,两个链表的长度不一定相等,所以就要解决它们的长度差。
  50. 一个字概述这个解法:骚。
  51. */
  52. public ListNode getIntersectionNode3(ListNode headA, ListNode headB) {
  53. if (headA==null||headB==null)
  54. return null;
  55. ListNode pA=headA;
  56. ListNode pB=headB;
  57. while (pA!=pB){
  58. pA=pA==null?headB:pA.next;
  59. pB=pB==null?headA:pB.next;
  60. }
  61. return pA;
  62. }
  63. }

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. echars 饼状图 轮循 水平翻转

    code: <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF ...

  2. 【php】day01

    一.PHPCORE基础 1.什么是PHP:[Hypertext Preprocessor]            WEB程序开发语言,运行在服务器端                         的 ...

  3. C++中二分法之upper_bound()、lower_bound、binary_search()函数

    前言 数组.容器vector都适用,在头文件"algorithm"中 下面的例子是针对容器的,注意返回的是距离元素3最近的指针it,输出的是*it结果为元素4,假如我想得到位置而非 ...

  4. Linux性能优化实战学习笔记:第四十六讲

    一.上节回顾 不知不觉,我们已经学完了整个专栏的四大基础模块,即 CPU.内存.文件系统和磁盘 I/O.以及网络的性能分析和优化.相信你已经掌握了这些基础模块的基本分析.定位思路,并熟悉了相关的优化方 ...

  5. WPF CoboxItem控件使用SelectedItem去调System.Windows.Controls.ComboBoxItem: 前缀方法

    textComBox.SelectedItem as ComboBoxItem).Content textConbox: 控件Combobox 的Name 在Combobox控件SelectionCh ...

  6. [转载]3.1 UiPath鼠标操作元素的介绍和使用

    一.鼠标(mouse)操作的介绍 模拟用户使用鼠标操作的一种行为,例如单击,双击,悬浮.根据作用对象的不同我们可以分为对元素的操作.对文本的操作和对图像的操作 二.鼠标对元素的操作在UiPath中的使 ...

  7. Intellij插件之MavenHelper

    作用: 一键查看maven依赖,查看冲突的依赖,一键进行exclude依赖 插件提供地址: https://plugins.jetbrains.com/plugin/7179-maven-helper ...

  8. CAP带你轻松玩转ASP.NETCore消息队列

    CAP是什么? CAP是由我们园子里的杨晓东大神开发出来的一套分布式事务的决绝方案,是.Net Core Community中的第一个千星项目(目前已经1656 Start),具有轻量级.易使用.高性 ...

  9. Docker使用compose(原Fig)快速编配

    Docker使用compose(原Fig)快速编配 目录 安装 应用 构建以及运行 安装 在Linux上安装Fig: 在OS上安装: 在Linux上安装Fig: sudo bash-c "c ...

  10. 折腾linux随笔 之 关闭Budgie默认自动隐藏应用的菜单栏 与 Gnome系桌面应用菜单无内容解决

    关闭Budgie默认自动隐藏应用菜单栏 首选项 -> 设置 -> 通用辅助功能 -> 打开 始终显示通用辅助菜单 后的开关 -> 注销桌面重新登录. done. 解决Gnome ...