160. Intersection of Two Linked Lists
Write a program to find the node at which the intersection of two singly linked lists begins.
For example, the following two linked lists:
A: a1 → a2
↘
c1 → c2 → c3
↗
B: b1 → b2 → b3
begin to intersect at node c1.
Notes:
- If the two linked lists have no intersection at all, return
null. - The linked lists must retain their original structure after the function returns.
- You may assume there are no cycles anywhere in the entire linked structure.
- Your code should preferably run in O(n) time and use only O(1) memory.
代码如下:
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) {
* val = x;
* next = null;
* }
* }
*/
public class Solution {
public ListNode getIntersectionNode(ListNode headA, ListNode headB) { if(headA==null||headB==null)
return null;
Map<ListNode,ListNode> map=new <ListNode,ListNode>HashMap(); while(headA!=null||headB!=null)
{ if(map.containsKey(headA)&&headA!=null)
return headA;
else
if(!map.containsKey(headA)&&headA!=null)
{
map.put(headA,headA);
headA=headA.next;
} if(map.containsKey(headB)&&headB!=null)
return headB;
else
if(!map.containsKey(headB)&&headB!=null)
{
map.put(headB,headB);
headB=headB.next;
} } return null;
}
}
160. Intersection of Two Linked Lists的更多相关文章
- 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 ...
- ✡ 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 ...
随机推荐
- function gzdecode
function gzdecode($data) { return gzinflate(substr($data,10,-8)); } 因为项目要下载一个gzip压缩的网页,所以需要解压,手册上有一个 ...
- 高效率JAVA实现斐波那契
import java.util.Scanner;public class Solution { public static int Fibonacci(int n) { int first = 0, ...
- JavaScript编程异步助手:Promise
异步模式在Web编程中变得越来越重要,对于Web主流语言JavaScript来说,这种模式实现起来不是很利索,为此,许多JavaScript库(比如 jQuery和Dojo.AngularJS)添加了 ...
- 加强版for循环
/*加强版for循环 * 5.0以后有加强版for循环 * for(String name:nameArray){} * 1.String name:声明会带有数组单一元素的循环变量 * 数组元素 ...
- 学会使用Ogitor
这几天在用Ogre读取Ogitor的场景,遇到了不少问题,在网上也找不到详细的说明,虽然读取Ogitor的场景对很多人来说太简单了,但对一些新手来说就有点难了,我刚开始就觉得是无从下手,因此简单的描述 ...
- Qemu+gdb跟踪内核源码
1.编译安装Qemu Qemu源码下载地址:http://wiki.qemu.org/Download linux下可以直接用wget下载: wget http://wiki.qemu.org/dow ...
- Interview---一道有趣的推理题
题目描述: 一个岛上有100个人,他们的眼睛只有两种颜色,蓝色和红色.95个人是黑色,其余5人是红色. 他们有个宗教信仰,从不照镜子,所以他们自己不知道自己的眼睛的颜色.但是能看到其他人的眼睛. 他们 ...
- 51 nod 机器人走方格
从一个长方形的方格的右上角 走到 左下角 , 问一共有多少种不同的路线可以达到 . #include<stdio.h> #include<string.h> #include& ...
- office2010安装Microsoft Office Document Imaging (MODI) 图解
office2010安装Microsoft Office Document Imaging (MODI) 图解 Microsoft Office 2010 中删除了 Microsoft Off ...
- SQL语句建表、设置主键、外键、check、default、unique约束
· 什么是数据库? 存放数据的仓库. · 数据库和数据结构有什么区别? 数据结构要解决在内存中操作数据的问题,数据库要解决在硬盘中操作数据的问题.数据结构研究一些抽象数据模型(ADT)和以及定义在该模 ...