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.

可以将A,B两个链表看做两部分,交叉前与交叉后。例子中

交叉前A:          a1 → a2

交叉前B:   b1 → b2 → b3

交叉后AB一样: c1 → c2 → c3

所以 ,交叉后的长度是一样,所以,交叉前的长度差即为总长度差。

只要去除长度差,距离交叉点就等距了。

 public class Solution {
public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
int lenA=0 ,lenB=0;
int dis;
for(ListNode x = headA;x != null;x = x.next) lenA++; //len of a
for(ListNode x = headB;x != null;x = x.next) lenB++;// len of b
dis = lenB - lenA;//distence of a and b if(dis > 0)//b is longer than a move headb
for(int i = dis;i > 0;i --) headB = headB.next;
else // a is longer than b move heada
for(int i = -dis;i > 0;i --) headA = headA.next; while(headA != headB){ // heada and headb are equal?
headA = headA.next;
headB = headB.next;
}
return headA;
}

参考:

http://www.cnblogs.com/ganganloveu/p/4128905.html

[LeetCode]160.Intersection of Two Linked Lists(2个链表的公共节点)的更多相关文章

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

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

  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 (两个链表的交点)

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

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

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

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

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

随机推荐

  1. Serlvet学习笔记之四—对文件的操作

    1.读文件 package com.demo; import java.io.BufferedReader; import java.io.FileReader; import java.io.Pri ...

  2. 计算时间:一个C++运算符重载示例

    Time类是一个用于计算时间的类,其原型如下:程序清单11.1 mytime0.h // mytime0.h -- Time class before operator overloading #if ...

  3. (一)微信小程序之模拟调用后台接口踩过的坑

    如下图标记的三个点 在调试过程中出现问题,特此记录. 1. 之前在浏览器测试接口习惯省略 http:// ,是因为浏览器默认有一个检测,在你输入的网址前面加http://,如果有就不加. 然而在微信小 ...

  4. JAVA Comparator 接口排序用法

    java的比较器有两类,分别是Comparable接口和Comparator接口. 在为对象数组进行排序时,比较器的作用非常明显,首先来讲解Comparable接口. 让需要进行排序的对象实现Comp ...

  5. 《转》python学习(8)元组

    转自 http://www.cnblogs.com/BeginMan/p/3156235.html 一.元组特性 1.类似列表,但不可变类型,正因如此,它可以做一个字典的key2.当处理一组对象时,这 ...

  6. c++11——模板的细节改进

    c++11改进了编译器的解析规则,尽可能的将多个右尖括号(>)解析为模板参数结束符,方便编写模板相关的代码. 1. 模板的右尖括号 之前的c++标准中,模板套模板中右尖括号不能连在一块,否则会和 ...

  7. 【BZOJ3529】[Sdoi2014]数表 莫比乌斯反演+树状数组

    [BZOJ3529][Sdoi2014]数表 Description 有一张N×m的数表,其第i行第j列(1 < =i < =礼,1 < =j < =m)的数值为能同时整除i和 ...

  8. 【BZOJ3275】Number 最小割

    [BZOJ3275]Number Description 有N个正整数,需要从中选出一些数,使这些数的和最大.若两个数a,b同时满足以下条件,则a,b不能同时被选1:存在正整数C,使a*a+b*b=c ...

  9. 点击TextView 弹出复制选项

    extends:http://www.eoeandroid.com/thread-226805-1-1.html package com.dotfive.chuanbang.view; import ...

  10. 分布式数据库主键id生成策略

    分布式数据库部署主要分为两种,一种是读写分离.这个需要弄主从数据库.主要是写的时候写主数据库,读的时候读从数据库.分散读取压力,对于读多写少的系统有利于 提高其性能.还有一种是分布式存储,这种主要是将 ...