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. Genymotion常见问题整合与解决方案

    常见问题1:Genymotion在开启模拟器时卡在了starting virtual device(注意只有tarting virtual device窗口,没有模拟器的黑屏窗口)    原因:Vir ...

  2. 动态链接库dll键盘钩子后台记录代码示例

    //.header #ifndef _DLLHOOK_H_ #define _DLLHOOK_H_ #include <windows.h> #define DLL_EXPORT_FUN ...

  3. 一模 (4) day2

    第一题: 题目大意:二进制数 n mod m 的结果是多少?  n 的长度(二进制数的位数)<=200 000:  m 的长度(二进制数的位数)<=20. 解题过程: 1.我的算法是直接高 ...

  4. left join 多表关联查询

    A表--left join-- B表  --on--A和B表相等的字段--  此时AB已关联 --left join--C表--on --A(或B)与C表相等的字段 此时ABC已关联 --left j ...

  5. NLTk

    1.python的nltk中文使用和学习资料汇总帮你入门提高 http://blog.csdn.net/huyoo/article/details/12188573

  6. struts2 文件的上传下载 表单的重复提交 自定义拦截器

    文件上传中表单的准备 要想使用 HTML 表单上传一个或多个文件 须把 HTML 表单的 enctype 属性设置为 multipart/form-data 须把 HTML 表单的method 属性设 ...

  7. OCR技术

    "起初我写这篇教程是在情人节,OCR可以带给你一整年的爱". 你之前肯定已经见过,OCR技术被应用于在平板电脑上将扫描文件处理成手写字迹,还被应用于谷歌最近添加到他们的Transl ...

  8. 从协议VersionedProtocol开始4——AdminOperationsProtocol、InterTrackerProtocol、JobSubmissionProtocol、TaskUmbilicalProtocol

    1.package org.apache.hadoop.mapred这四个协议都在这个包下. 2.从最简单的AdminOperationsProtocol看, void refreshQueues() ...

  9. grub2的使用

    1,添加win 启动项 edit file: /boot/grub2/grub.cfg 插入这几行: menuentry 'Windows XXX' { set root=(hd0,) chainlo ...

  10. The authenticity of host 192.168.0.xxx can't be established.

    用ssh登录一个机器(换过ip地址),提示输入yes后,屏幕不断出现y,只有按ctrl + c结束 错误是:The authenticity of host 192.168.0.xxx can't b ...