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.

如果两个链表有重复的部分,那么返回重复的起始位置,否则,返回null。

两种方法:

1、(参考discuss中,并非最佳答案)

可以利用hashMap,先把第一个链表的所有节点放入map中,然后再遍历第二个链表,看map中是否有相同的节点。如果没有,返回null。

时间复杂度O(m+n),空间复杂度O(m) or O(n)

2、先遍历一次链表,找出连个链表的最后一个节点(如果不一样,那么返回null)以及长度差(最佳)

然后较长的链表先走长度差个节点,

之后两个链表一起走,遇到相同的就返回。

时间复杂度O(m+n),空间复杂度O(1)

/**
* 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) {
int len1 = 1,len2 = 1;
if( headA == null || headB == null)
return null;
ListNode node1 = headA;
ListNode node2 = headB; while( node1.next != null ){
node1 = node1.next;
len1++;
}
while( node2.next != null ){
node2 = node2.next;
len2++;
}
if( len1 == 0 || len2 == 0 || node1 != node2 )
return null;
node1 = headA;
node2 = headB;
if( len1 > len2 ){
while( len1 > len2 ){
len1--;
node1 = node1.next;
}
}else if( len1 < len2 ){
while( len1<len2){
len2--;
node2 = node2.next;
}
} while( len1 >0 ){
if( node1 == node2 )
return node1;
node1 = node1.next;
node2 = node2.next;
len1--;
}
return null;
}
}

3、(参考discuss)

不用第一次遍历找出长度差,一直循环遍历找出相同的节点(如果不存在会出现null)

/**
* 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) {
//boundary check
if(headA == null || headB == null) return null; ListNode a = headA;
ListNode b = headB; //if a & b have different len, then we will stop the loop after second iteration
while( a != b){
//for the end of first iteration, we just reset the pointer to the head of another linkedlist
a = a == null? headB : a.next;
b = b == null? headA : b.next;
} return a;
}
}

✡ leetcode 160. Intersection of Two Linked Lists 求两个链表的起始重复位置 --------- java的更多相关文章

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

  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] Intersection of Two Linked Lists 求两个链表的交点

    Write a program to find the node at which the intersection of two singly linked lists begins. For ex ...

  4. [LintCode] Intersection of Two Linked Lists 求两个链表的交点

    Write a program to find the node at which the intersection of two singly linked lists begins. Notice ...

  5. [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(2个链表的公共节点)

    Intersection of Two Linked Lists Write a program to find the node at which the intersection of two s ...

  7. [LeetCode]160. Intersection of Two Linked Lists判断交叉链表的交点

    方法要记住,和判断是不是交叉链表不一样 方法是将两条链表的路径合并,两个指针分别从a和b走不同路线会在交点处相遇 public ListNode getIntersectionNode(ListNod ...

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

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

随机推荐

  1. WP8 学习 在APP.XAML中加入Resources

    <Application.Resources> <local:LocalizedStrings xmlns:local="clr-namespace:test1" ...

  2. xcode6 ios launchimage

    1.点击Image.xcassets 进入图片管理,然后右击,弹出"New Launch Image" 2.右侧的勾选可以让你选择是否要对ipad,横屏,竖屏,以及低版本的ios系 ...

  3. win10环境下使用苹果虚拟机不要开多线程应用下载文件

    win10环境下使用苹果虚拟机开多线程应用下载文件时候卡死,网络老掉. 8GB内存不够用?2.5mb网速不够用? 开的百度网盘下载个电影 结果虚拟机卡的不行 关了 网盘 挂起虚拟机 然后再 继续运行客 ...

  4. 【转】linux下如何查看某个软件 是否安装?安装路径在哪

    以redhat\centos 中php-mysql为例1:如果包是通过yum或者rpm方式安装[root@localhost yum.repos.d]# rpm -qa //找出系统所有的包,找到对应 ...

  5. java运算符的优先级

    Java 编辑 运算符 结合性 [ ] . ( ) (方法调用) 从左向右 ! ~ ++ -- +(一元运算) -(一元运算)  从右向左 * / % 从左向右 + - 从左向右 << & ...

  6. BinaryWriter

    c#里的文件操作 fileInfo dir的一大堆属性不用说 地球人都知道(什么fileName,create() delete()) ,文件系统的概念很好理解的 文件读写也好理解(硬盘到内存 然后再 ...

  7. jenkins持续集成工具

    jenkins是开源的 持续集成.持续构建的可视化web工具,持续构建说直白,就是各种项目的自动化编译.打包.分发部署. 好很多优点: 1.支持多语言 2.跟svn.git能无缝集成 3.直接与知名源 ...

  8. 利用NTFS交换数据流隐藏文件

    利用NTFS交换数据流隐藏文件 发表于 2012 年 12 月 15 日 由 晴刃 这篇文章介绍一下Windows的NTFS文件系统的ADS(alternate data streams,交换数据流) ...

  9. intent 传参数

    一.传递List<String>和List<Integer>以下以传递List<String>为例,发送List<String>语法为:intent.p ...

  10. 关于oracle存储过程的一些知识点

    一.创建一个存储过程,批量清空数据库中所有表的数据. --清空数据库中所有表的数据 create or replace procedure truncateAllTables as v_sql ); ...