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的更多相关文章

  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. ✡ 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 ...

  7. 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. ...

  8. 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 ...

  9. 【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. 改变Chrome浏览器主程序_缓存_个人信息路径

      改变Chrome浏览器缓存_个人信息路径(亲测) actionx2上传于2012-10-26|(7人评价)|3077人阅读|41次下载|文档简介|举报文档    在手机打开   改变 Chrom ...

  2. C语言:文件操作

    以附加方式打开文件,输入数据,关闭文件. #include<stdio.h> #include<stdlib.h> int main() { FILE *fp = NULL; ...

  3. 基于K2 BPM平台,中原地产实现了从2个人到5万多人的跨越

    演讲人:吴付文 中原地产CIO 点击这里查看中原地产怎么使用BPM实现业绩的飞跃式发展.

  4. android中正确导入第三方jar包

    android中正确导入第三方jar包 andriod中如果引入jar包的方式不对就会出现一些奇怪的错误. 工作的时候恰好有一个jar包需要调用,结果用了很长时间才解决出现的bug. 刚开始是这样引用 ...

  5. SQL Server 2005使用作业设置定时任务(转)

    1.开启SQL Server Agent服务 使用作业需要SQL Agent服务的支持,并且需要设置为自动启动,否则你的作业不会被执行. 以下步骤开启服务:开始-->>>运行--&g ...

  6. JavaScript之document对象使用

    1.document 对象常用的有三种: A.document.getElementById:通过html元素的Id,来获取html对象.适用于单个的html元素. B.document.getEle ...

  7. iOS上架(转)

    自己的经验总结,有错的话请留言,第一时间更改. 先大概说一下IOSAPP上架的几个步骤(详细步骤见下图): 创建证书请求文件 登录苹果开发者中心生成发布者证书(下载下来要双击一下) 设置APPID(要 ...

  8. Mysql5.0以下 手工注入

    order by 20 www. .com/product/introduction.php?id=-65 UNION SELECT user(),2 www. .com/product/introd ...

  9. 详解模块定义(.def)文件

    一个完整的Windows应用程序(C++程序)通常由五种类型的文件组成:源程序文件,头文件,资源描述文件,项目文件,模块定义文件.本文主要讲解模块定义文件. 模块定义 (.def)文件为链接器提供有关 ...

  10. android-volley-at-a-glance

    http://liubin.org/2013/05/27/android-volley-at-a-glance/ http://www.androidhive.info/2014/05/android ...