LeetCode——Intersection of Two Linked Lists
Description:
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
begin to intersect at node c1.
求两个链表的相交的部分
/**
* 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) {
if(headA==null || headB==null)
return null;
ListNode aNode = headA;
ListNode bNode = headB;
int aLen = 0, bLen = 0;
while(aNode != null) {
aLen ++;
aNode = aNode.next;
}
while(bNode != null) {
bLen ++;
bNode = bNode.next;
}
if(aNode != bNode)
return null;
if(aLen > bLen) {
for(int i = 0; i < aLen-bLen; i++)
headA = headA.next;
}
else if(aLen < bLen) {
for(int i=0; i<bLen-aLen; i++)
headB = headB.next;
}
while(headA != headB) {
headA = headA.next;
headB = headB.next;
}
return headA;
}
}
LeetCode——Intersection of Two Linked Lists的更多相关文章
- 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 ...
- [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 ...
- LeetCode Intersection of Two Linked Lists
原题链接在这里:https://leetcode.com/problems/intersection-of-two-linked-lists/ 思路:1. 找到距离各自tail 相同距离的起始List ...
- [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 ...
- LeetCode Intersection of Two Linked Lists (找交叉点)
题意: 给两个链表,他们的后部分可能有公共点.请返回第一个公共节点的地址?若不重叠就返回null. 思路: 用时间O(n)和空间O(1)的做法.此题数据弱有些弱. 方法(1)假设两个链表A和B,用两个 ...
- [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 ...
- [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 ...
- [LintCode] Intersection of Two Linked Lists 求两个链表的交点
Write a program to find the node at which the intersection of two singly linked lists begins. Notice ...
- 2016.5.24——Intersection of Two Linked Lists
Intersection of Two Linked Lists 本题收获: 1.链表的输入输出 2.交叉链表:这个链表可以有交叉点,只要前一个节点的的->next相同即可. 题目:Inters ...
随机推荐
- [Makefile]多目录Makefile写法
最近需要写一个测试程序,这个测试程序需要集成一些功能,写在同一个文件看上去很不好,多个文件的Makefile又不是很熟,于是分享下面这篇文章 先介绍下,调试Makefile时,想知道某个变量的值,怎么 ...
- Android——实现欢迎界面的自动跳转(转)
Android实现欢迎界面的自动跳转,就是打开某一个安卓手机应用,出现的欢迎界面停留几秒钟,自动进入应用程序的主界面.在网上看到很多种实现办法,但是感觉这种方法还是比较简单的. 在onCreate里设 ...
- 在XP系统下如何访问win10共享的打印机
< > 找到 GUEST 用户,添加即可. 2. Win10 共享本地打印机 右击要共享的打印机,共享选项卡,设置共享名,这个共享很重要,要记住,尽量设置简单点.IP + 共享名就是网络打 ...
- MapReduce 学习(一)
首先我们先来欣赏一下MapReduce的执行过程吧,如下图,自己看,不解释了. Map 和 Reduce 的处理都是基于Key/Value来进行的,在Map中对文件的每一行进行处理,有两个输入参数,K ...
- 七、CentOS 6.5 下 Nginx的反向代理和负载均衡的实现
CentOS 6.5 下 Nginx的反向代理和负载均衡的实现 * 修复上面文章的问题: 复制出一个tomcat2之后,修改service.xml文件时,要修改三个端口: 1. <!-- 800 ...
- java- Servlet-session
Servlet 相关:http://blog.csdn.net/ggGavin/article/category/2134213 Servlet——Session(1)之基础知识 Servlet——S ...
- jar/war/ear文件的区别
jar/war/ear三种文件,在文件结构上,三者并没有什么不同,它们都采用zip或jar档案文件压缩格式.但是它们的使用目的有所区别: Jar文件(扩展名为. Jar)包含Java类的普通库.资源 ...
- 第二百八十五节,MySQL数据库-MySQL函数
MySQL数据库-MySQL函数 1.MySQL内置函数 SELECT执行函数,后面跟要执行的函数 CHAR_LENGTH(str)函数:返回字符串的字符长度 -- CHAR_LENGTH(str)函 ...
- 无法从“重载函数类型”为“const std::_Tree<_Traits> &”推导 <未知> 参数
场景: 原因: 用到string类型,但是没有包含头文件. 解决方法: #include<string>
- e638. 向剪切板获取和粘贴图像
// If an image is on the system clipboard, this method returns it; // otherwise it returns null. pub ...