Intersection of Two Linked Lists 解答
Question
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.
Notes:
- If the two linked lists have no intersection at all, return
null
. - The linked lists must retain their original structure after the function returns.
- You may assume there are no cycles anywhere in the entire linked structure.
- Your code should preferably run in O(n) time and use only O(1) memory.
Solution
Key to the solution here is to traverse two lists to get their lengths. Therefore, we can move the pointer for the longer list first and then compare elements of both lists.
/**
* 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 p1 = headA, p2 = headB;
int l1 = 0, l2 = 0, ll = 0;
while (p1 != null) {
l1++;
p1 = p1.next;
}
while (p2 != null) {
l2++;
p2 = p2.next;
}
p1 = headA;
p2 = headB;
if (l2 >= l1) {
ll = l2 - l1;
while (ll > 0) {
p2 = p2.next;
ll--;
}
} else {
ll = l1 - l2;
while (ll > 0) {
p1 = p1.next;
ll--;
}
}
while (p1 != null && p2 != null) {
if (p1.val == p2.val)
return p1;
p1 = p1.next;
p2 = p2.next;
}
return null;
}
}
Intersection of Two Linked Lists 解答的更多相关文章
- [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: 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]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 ...
- [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 ...
随机推荐
- 原生javascript添加引用js文件
function addScriptTag(src) { var script = document.createElement(&qu ...
- chrome无法使用非官方商店扩展解决办法
自己开发的工具性插件不想放在官方商店(当然也有可能是工作相关的工具不能放在官方商店),由于新版本的chrome不允许非官方商店的插件进行安装使用,所以出现一个头疼的问题:每次开启chrome都 ...
- Redis 该选择哪种持久化配置
这个标题或许会让你想起<黑客帝国>里经典的台词,你要选择蓝色药丸,还是红色药丸? Redis 是我们重度使用的一个开源软件,对它的持久化配置做一番相对深入的总结,是值得的.目前它有两种主流 ...
- 关于MemoryBarrier
备注:OSG OpenThread::Atomic.cpp中MemoryBarrier(); Atomic::operator unsigned() const { #if defined(_OPE ...
- linux 常用 命令 笔记二
wget 下载,得到网络上的内容 grep 文件搜索工具 EveryThing is a file in the linux system 安装 cowsay sudo apt-get install ...
- 曾经很长时间不会写的两个SQL语句(group by,having)
1.统计各部门的平均工资,平均资金(要求同时显示出部门名称,部门编号,部门总人数) SQL)),) 部门平均工资,)),) 部门平均资金 FROM EMP E RIGHT JOIN DEPT D ON ...
- JS数组追加数组采用push.apply的坑(转)
JS数组追加数组没有现成的函数,这么多年我已经习惯了a.push.apply(a, b);这种自以为很酷的,不需要写for循环的写法,一直也没遇到什么问题,直到今天我要append的b是个很大的数组时 ...
- JavaScript-------寄生组合式继承
组合继承在前面有说过,也是JavaScript中最常用的一个继承模式:不过,它也有自己的不足.组合继承最大的问题就是无论什么情况,都会调用两次构造函数: 那我们来回顾下组合式继承基本模式: funct ...
- C#使用seleium实现一个自动登录器
1.http://docs.seleniumhq.org/ 下载seleium包 2.新建一个C#项目,比如控制台,引用seleium包中的dll using System; using System ...
- 关于iOS9之后的loadViewIfNeeded
iOS9之前 有些时候因为需要手动调用loadview 但是有风险,系统不再调用viewDidLoad 所以手动调用loadview是错误的 iOS9之后出现了loadViewIfNeeded解决了这 ...