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
求两个链表开始重合的地方,也就是交叉的地方。。
思路:两个链表从交叉的位置开始,后面的长度是相同的,前面的长度可能相等也可能不同。当前面长度也相同,也就是两个链表长度相同时,就一一对应比较,找出相同的节点。长度相同时,只有一一对应的位置才可能相同,交错的位置上节点是不可能相同的,因为若交错的位置节点相同,那么后面长度要相同,因为出现交错,前面长度就不相同了,所以不行。。。当两个链表长度不相同时,从交叉点开始,后面长度相等,所以只有交叉点前面长度会不同,而这对求交叉点没有影响,我们只要跳过长链表的头上几个节点,使前面长度也相同,这样就可以开始一一对应比较了,只有长度相同才可以一一对应比较。而长链表前面比短链表多出来的几个节点对求交叉点是没有影响的(如上面的b1)。交叉点是不会出现在那几个节点中的,因为如果出现在那里,这样交叉点以后都是重复的,这样重复的节点个数都大于短链表的节点个数了。
/**
* 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 lenA=length(headA),lenB=length(headB); //从头开始移到长度相同的位置,因为多出来的那些元素是不会出现相等的,如果出现相等(就开始重合),长度就比另一个链表长了,这是不正确的。
while(lenA<lenB){
headB=headB.next;
lenB--;
} while(lenA>lenB){
headA=headA.next;
lenA--;
}
//长度相同,开始一一比对
while(headA!=headB){
headA=headA.next;
headB=headB.next;
}
//如果没有重合的,一直到最后,headA和headB都会为空了,上面也会跳出循环
return headA;
} public int length(ListNode node){
int length=0;
while(node!=null){
length++;
node=node.next;
}
return length;
}
}
intersection of two linked lists.(两个链表交叉的地方)的更多相关文章
- 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 ...
- lintcode 中等题:Intersection of Two Linked Lists 两个链表的交叉
题目 两个链表的交叉 请写一个程序,找到两个单链表最开始的交叉节点. 样例 下列两个链表: A: a1 → a2 ↘ c1 → c2 → c3 ↗ B: b1 → b2 → b3 在节点 c1 开始交 ...
- [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 ...
- [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 ...
- Intersection of Two Linked Lists两链表找重合节点
Write a program to find the node at which the intersection of two singly linked lists begins. For ex ...
- [LC]141题 Intersection of Two Linked Lists (相交链表)(链表)
①中文题目 编写一个程序,找到两个单链表相交的起始节点. 如下面的两个链表: 在节点 c1 开始相交. 注意: 如果两个链表没有交点,返回 null.在返回结果后,两个链表仍须保持原有的结构.可假定整 ...
- [LintCode] Intersection of Two Linked Lists 求两个链表的交点
Write a program to find the node at which the intersection of two singly linked lists begins. Notice ...
- [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 ...
- 2016.5.24——Intersection of Two Linked Lists
Intersection of Two Linked Lists 本题收获: 1.链表的输入输出 2.交叉链表:这个链表可以有交叉点,只要前一个节点的的->next相同即可. 题目:Inters ...
随机推荐
- J2EE进阶(八)Hibernate与延迟加载机制探究
Hibernate与延迟加载机制探究 前言 Hibernate对象关系映射提供延迟的与非延迟的对象初始化.非延迟加载在读取一个对象的时候会将与这个对象所有相关的其他对象一起读取出来.这有时会导致成百的 ...
- 偏置方差分解Bias-variance Decomposition
http://blog.csdn.net/pipisorry/article/details/50638749 偏置-方差分解(Bias-Variance Decomposition) 偏置-方差分解 ...
- Java数组的应用:案例:杨辉三角,三维数组,字符串数组
//import java.util.Arrays; //包含Arrays //import java.util.Random; public class HelloWorld { public st ...
- Swift下自定义xib添加到Storyboard
猴子原创,欢迎转载.转载请注明: 转载自Cocos2Der-CSDN,谢谢! 原文地址: http://blog.csdn.net/cocos2der/article/details/51657154 ...
- FSG报表打印报错,log文件显示java.sql.SQLException: No corresponding LOB data found
报错信息: +---------------------------------------------------------------------------+ Plsql 程序的日志信息开始 ...
- [Django] 单元测试小记
从前很少写单元测试了,特别是web应用.最近不知不觉喜欢起来这个事情了,发现单元测试对于软件的模块,正交性有很大促进作用,因为函数,模块写的不合理,单元测试写起来就麻烦的多呀.公司的项目一直都是用Dj ...
- 【一天一道LeetCode】#41. First Missing Positive
一天一道LeetCode系列 (一)题目 Given an unsorted integer array, find the first missing positive integer. For e ...
- MySQL的一些常用的SQL语句整理
安装MySQL有两种的方式,一种是解压版本,但是需要配置环境变量,相对而言比较麻烦.所以我们一般采取第二种方式,那就是到MySQL的官网上下载安装版.这样就会省去很多麻烦,在这里我就不再详细的介绍具体 ...
- 【算法导论】最小生成树之Prime法
关于最小生成树的概念,在前一篇文章中已经讲到,就不在赘述了.下面介绍Prime算法: 其基本思想为:从一个顶点出发,选择由该顶点出发的最小权值边,并将该边的另一个顶点包含进来,然后找出 ...
- mysql语法、特殊符号及正则表达式的使用
http://blog.csdn.net/pipisorry/article/details/46773545 sql语言 结构化的查询语言.(Structured Query Language),是 ...