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. python2.0_day19_后台数据库设计思路

    from django.db import models # Create your models here. from django.contrib.auth.models import User ...

  2. lower()

    lower() 用于把字符串中的大写字母转换成小写字母 In [1]: str = "Hello World" In [2]: str.lower() Out[2]: 'hello ...

  3. View的setTag()与getTag()方法使用

    通常我们是用findViewById()方法来取得我们要使用的View控件,不过除了这一种方法之处 ,我们还可以用View中的setTag(Onbect)给View添加一个格外的数据,再用getTag ...

  4. poj_3579 二分法

    题目大意 给定N个数,这些数字两两求差构成C(N,2)(即N*(N-1)/2)个数值,求这C(N,2)个数的中位数.N <= 100000. 题目分析 根据数据规模N最大为100000,可知不能 ...

  5. iGson

    头文件 #import <Foundation/Foundation.h> #import <objc/runtime.h> #import "NSString+Ut ...

  6. android基础---->AIDL服务的使用

    AIDL和其他的IDL类似,它允许你定义程序接口,以便客户端与服务器端通过IPC机制交互.在android上面,一个进程一般不能访问另外进程的内存.因此,Android平台将这些跨进程访问的对象分解成 ...

  7. JS-比较函数中嵌套函数,可以排序【对象数组】

    function createCompareFun(propertyName){ return function(object1,object2){ var value1 = object1[prop ...

  8. window.location.href和document.location.href、document.URL的区别

    1.document表示的是一个文档对象,window表示的是一个窗口对象,一个窗口下可以有多个文档对象. 所以一个窗口下只有一个window.location.href,但是可能有多个documen ...

  9. 关于VO、PO的理解——java的(PO,VO,TO,BO,DAO,POJO)解释

    O/R Mapping 是 Object Relational Mapping(对象关系映射)的缩写.通俗点讲,就是将对象与关系数据库绑定,用对象来表示关系数据. 在O/R Mapping的世界里,有 ...

  10. mysql load data导入脚本

    # !/bin/bash ############中文说明###################### #本程序的一些提示需要中文支持,如系统没有安装中文包,请先安装:yum -y groupinst ...