Intersection of Two Linked Lists(java)
eg: a1 → a2
↘
c1 → c2 → c3 或者直接a1 → b1
↗
b1 → b2 → b3
求公共链表c1. 常规的指针分裂法,复制法,偏移法。
public class Solution {
public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
if(headA==null||headB==null) return null;
int lengthA=0;
int lengthB=0;
ListNode currentA=headA;
ListNode currentB=headB;
while(currentA.next!=null)
{
currentA=currentA.next;
lengthA++;
}
while(currentB.next!=null)
{
currentB=currentB.next;
lengthB++;
}
ListNode fast=lengthA>lengthB?headA:headB;
ListNode slow=lengthA>lengthB?headB:headA;
for(int i=0;i<Math.abs(lengthA-lengthB);i++)
{
fast=fast.next;
} while(fast!=null)
{
if(fast==slow)
return fast;
else
{
fast=fast.next;
slow=slow.next;
}
}
return null;
}
}
Intersection of Two Linked Lists(java)的更多相关文章
- LeetCode: Intersection of Two Linked Lists 解题报告
Intersection of Two Linked Lists Write a program to find the node at which the intersection of two s ...
- [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 ...
- [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 ...
- 160. Intersection of Two Linked Lists【easy】
160. Intersection of Two Linked Lists[easy] Write a program to find the node at which the intersecti ...
- LeetCode_160. Intersection of Two Linked Lists
160. Intersection of Two Linked Lists Easy Write a program to find the node at which the intersectio ...
- LeetCode--LinkedList--160. Intersection of Two Linked Lists(Easy)
160. Intersection of Two Linked Lists(Easy) 题目地址https://leetcode.com/problems/intersection-of-two-li ...
- 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 ...
随机推荐
- 不用position,让div垂直居中
先弄懂after伪类的用法,就可以很容易理解了. <!DOCTYPE html> <html lang="en"><head> <meta ...
- iOS学习资料整理
视频教程(英文) 视频 简介 Developing iOS 7 Apps for iPhone and iPad 斯坦福开放教程之一, 课程主要讲解了一些 iOS 开发工具和 API 以及 iOS S ...
- jquery动态添加DOM节点
1.append()方法:向每个匹配的元素内部添加元素 appendTo()方法:将所有匹配的元素追加的指定的元素中 <html> <head> <meta http-e ...
- Spark集群搭建简要
Spark集群搭建 1 Spark编译 1.1 下载源代码 git clone git://github.com/apache/spark.git -b branch-1.6 1.2 修改pom文件 ...
- mac上访问samba服务器
打开safari输入smb://ip,回车后出现输入用户名密码对话框,若是匿名则选择作为“客人”选项 例子 smb://192.168.2.3
- Python爬虫学习:二、爬虫的初步尝试
我使用的编辑器是IDLE,版本为Python2.7.11,Windows平台. 本文是博主原创随笔,转载时请注明出处Maple2cat|Python爬虫学习:二.爬虫的初步尝试 1.尝试抓取指定网页 ...
- 关于hibernate非主键多对一关联
一直以来,用hibernate做多对一时,关联的都是主键,今天,同事设计的表,关联的却不是主键,这下麻烦了,hibernate的many-to-one默认关联的都是主键,原来,hibernate提供了 ...
- AJAX Data 传值 无效的JSON基元:AJAX jQuery的方法,用c#WEBMETHOD-c#,jquery.
我有这样的代码:I数据值,而不是连接字符串作为对象常量.为什么?看到这里 我的代码是这样的:- $.ajax({ url: "../Member/Home.aspx/SaveClient&q ...
- Extjs4 Grid内容已经拿到但是不显示数据
原先照着Extjs4.0.7官方文档写了一个GridPanel的列子,没有什么问题,今天又自己写了一个,效果如下,内容肯定拿到就是不显示: 经过一段代码排查后,问题出在了自定义Model时将field ...
- 【转】内核编译时, 到底用make clean, make mrproper还是make distclean(转载)
原文网址:http://dongyulong.blog.51cto.com/1451604/449470 内核编译时, 到底用make clean, make mrproper还是make distc ...