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 ...
随机推荐
- echars 饼状图 轮循 水平翻转
code: <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF ...
- 【php】day01
一.PHPCORE基础 1.什么是PHP:[Hypertext Preprocessor] WEB程序开发语言,运行在服务器端 的 ...
- C++中二分法之upper_bound()、lower_bound、binary_search()函数
前言 数组.容器vector都适用,在头文件"algorithm"中 下面的例子是针对容器的,注意返回的是距离元素3最近的指针it,输出的是*it结果为元素4,假如我想得到位置而非 ...
- Linux性能优化实战学习笔记:第四十六讲
一.上节回顾 不知不觉,我们已经学完了整个专栏的四大基础模块,即 CPU.内存.文件系统和磁盘 I/O.以及网络的性能分析和优化.相信你已经掌握了这些基础模块的基本分析.定位思路,并熟悉了相关的优化方 ...
- WPF CoboxItem控件使用SelectedItem去调System.Windows.Controls.ComboBoxItem: 前缀方法
textComBox.SelectedItem as ComboBoxItem).Content textConbox: 控件Combobox 的Name 在Combobox控件SelectionCh ...
- [转载]3.1 UiPath鼠标操作元素的介绍和使用
一.鼠标(mouse)操作的介绍 模拟用户使用鼠标操作的一种行为,例如单击,双击,悬浮.根据作用对象的不同我们可以分为对元素的操作.对文本的操作和对图像的操作 二.鼠标对元素的操作在UiPath中的使 ...
- Intellij插件之MavenHelper
作用: 一键查看maven依赖,查看冲突的依赖,一键进行exclude依赖 插件提供地址: https://plugins.jetbrains.com/plugin/7179-maven-helper ...
- CAP带你轻松玩转ASP.NETCore消息队列
CAP是什么? CAP是由我们园子里的杨晓东大神开发出来的一套分布式事务的决绝方案,是.Net Core Community中的第一个千星项目(目前已经1656 Start),具有轻量级.易使用.高性 ...
- Docker使用compose(原Fig)快速编配
Docker使用compose(原Fig)快速编配 目录 安装 应用 构建以及运行 安装 在Linux上安装Fig: 在OS上安装: 在Linux上安装Fig: sudo bash-c "c ...
- 折腾linux随笔 之 关闭Budgie默认自动隐藏应用的菜单栏 与 Gnome系桌面应用菜单无内容解决
关闭Budgie默认自动隐藏应用菜单栏 首选项 -> 设置 -> 通用辅助功能 -> 打开 始终显示通用辅助菜单 后的开关 -> 注销桌面重新登录. done. 解决Gnome ...