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 ...
随机推荐
- ROS_Kinetic_24 使用catkin_create_qt_pkg快速创建qt-ros功能包
使用catkin_create_qt_pkg快速创建qt-ros功能包 参考网址: qt_create:http://wiki.ros.org/qt_create qt_ros:https://git ...
- Android 5.1 添加硬件抽象层(HAL)和JNI接口总结
点击打开链接
- android 关机充电流程
点击打开链接 0.主要流程 usb插入通过传递cmdline给init解析从而启动充电进程 1. LK lk\app\aboot\aboot.c update_cmdline ---------- i ...
- Maven原型骨架及常见问题
关于Maven原型工程的制作就不在这里详细介绍了,具体细节请参考之前的文章:定制Maven原型生成项目 下面分享制作和使用Maven原型工程时碰到的常见问题,以及原型的上传和使用方法. 1.模块路径问 ...
- 排列熵算法简介及c#实现
一. 排列熵算法简介: 排列熵算法(Permutation Entroy)为度量时间序列复杂性的一种方法,算法描述如下: 设一维时间序列: 采用相空间重构延迟坐标法对X中任一元素x(i)进行相空间 ...
- 5.创建表,使用alter进行表信息的增删改,Oracle回收站,集合运算
1 Oracle基于用户的管理方案 2 DDL语句可以管理数据库的对象有:视图 索引 序列 同义词 约束 3 创建一个表,有2个条件(1 有权限:2有表空间) Oracle给你提 ...
- Cookie 进阶
Cookie作为一个客户端技术被广泛的应用着.我今天也来谈一谈我对Cookie的理解. 先来一个小菜(实现"上次登录时间") 具体的思路如下: 通过request.getCooki ...
- Get and Post(Unity3D开发之六)
猴子原创,欢迎转载.转载请注明: 转载自Cocos2D开发网–Cocos2Dev.com,谢谢! 原文地址: http://www.cocos2dev.com/?p=565 unity3d中的www直 ...
- R12: Improving Performance of General Ledger and Journal Import (Doc ID 858725.1 )
In this Document Purpose Scope Details A) Database Init.ora Parameters B) Concurrent Progr ...
- Leetcode_102_Binary Tree Level Order Traversal
本文是在学习中的总结,欢迎转载但请注明出处:http://blog.csdn.net/pistolove/article/details/41929059 Given a binary tree, r ...