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 ...
随机推荐
- MySQL设计软件登录模块
学了一段时间的Java了,思量着做一点简单的小模块的东西吧,于是就有了下面的这个简单的小案例. 大致实现的功能就是注册于登录还有就是用到了一点,分层思想.仅此而已,所以非常的适合新手围观. 建立好数据 ...
- Unity插件 - MeshEditor(六) 变形动画状态机
变形动画状态机--MeshAnimator,是针对MeshAnimation的状态管理器,有大量类似Unity animator的功能,但MeshAnimator操作会更加简便,更加直观,居家旅(zh ...
- 后端分布式系列:分布式存储-HDFS NameNode 设计实现解析
接前文 分布式存储-HDFS 架构解析,我们总体分析了 HDFS 架构的主要构成组件包括:NameNode.DataNode 和 Client.本文首先进一步解析 HDFS NameNode 的设计和 ...
- StringBuffer与StringBuilder详解
刚刚在参加网易实习生在线考试的时候,出了一道选择题谈到了StringBuilder这个类的一些选项,虽然那道题自己做对了,但是也提醒了我应该好好了解一些StringBuffer与StringBuild ...
- cocos2dx深度检测与Zorder
cocos2dx里面有两个渲染队列,RenderQueue和TransparentRenderQueue.我们可以从Renderer::render()的代码看到: void Renderer::re ...
- SpriteBuilder中节点位置类型为百分比时不能定位的解决
Ball.ccb类型是Node,其中有个子节点为Color Node,其中物理使能. MainScene.ccb中加入一个物理节点,将Ball.ccb拖入其中,成为该物理节点的孩子,这时出现了一个&q ...
- [leetcode]经典算法题- String to Integer (atoi)
题目描述: 把字符串转化为整数值 原文描述: Implement atoi to convert a string to an integer. Hint: Carefully consider al ...
- ARM v7汇编与相关练习
程序入口: _startc 语言入口: main@: 注释;main: 标签;伪指令: 给汇编器读的指令;.global main ...
- Java关键字之this
this的作用: 1) this是当前对象的一个引用,便于对当前对象参数的使用: 2)可以返回对象的自己这个类的引用,同时还可以在一个构造函数当中调用另一个构造函数 this示例: public cl ...
- msm8916 dt选用规则
1.AndroidBoard.mk 选则kernel build 默认配置文件:msm8916_defconfig /device/qcom/msm8916/AndroidBoard.mk #---- ...