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 ...
随机推荐
- DBCP连接池TestOnBorrow的坑
生产环境连接池TestOnBorrow设置为false,导致有时获取的连接不可用.分析如下: TestOnBorrow=false时,由于不检测池里连接的可用性,于是假如连接池中的连接被数据库关闭了, ...
- Simple tutorial for using TensorFlow to compute polynomial regression
"""Simple tutorial for using TensorFlow to compute polynomial regression. Parag K. Mi ...
- Java-IO之RandomAccessFile
RandomAccessFile是随机访问(读写)的类,支持对文件随机访问的读取和写入,也可以从指定的位置读取和写入文件数据.RandomAccessFile虽然属于java.io包,但它不是Inpu ...
- iOS中 Swift初级入门学习(一)
/ // Copyright (c) 2015年 韩俊强. All rights reserved. // import Foundation // Swift当中的输出函数 // println S ...
- mysql DISTINCT 的实现与优化
DISTINCT实际上和GROUP BY的操作非常相似,只不过是在GROUP BY之后的每组中只取出一条记录而已.所以,DISTINCT的实现和GROUP BY的实现也基本差不多,没有太大的区别.同样 ...
- TableEdit UI_10
1.tableView的编辑的步骤: 1.让tableView处于编辑状态,(默认所有的cell都处于编辑状态,默认下的编辑样式是删除) 2.设置哪些cell可以编辑 3.设置编辑的样式(删除,插 ...
- chrome "Provisional headers are shown"
我的问题的,每次打开浏览器,点开有视频的页面(云平台的存储),然后当视频还没有加载完,就马上关闭,连续操作很多次(测试的暴力测试把),F12查看就有很多很多在加载中的连接,因为连续点击太多次了,第一个 ...
- nginx 安装php
1. 安装PHP 5.5.0 下载 1 2 cd /usr/local/src/ wget http://www.php.net/get/php-5.5.0.tar.bz2/from/jp1.ph ...
- Dynamics CRM2011 同一个FORM表单同一个字段可以摆放多次
这是一个很细微的问题,细微到很容易被忽略(可能其他人没有但我确实是忽略了) 红框中的勾默认是勾选的,可能有人会像我一样,从没关心过这个地方还有个勾也从没关心过要把这个勾去掉,所以在表单上A.B两个节中 ...
- 聊聊javaMail
今天闲着无事 看了看用java发送邮件的相关知识 代码参考自<<精通Java Web整合开发(JSP+AJAX+Struts+Hibernate)>>(第2版) 不多说 先上图 ...