[Algorithm] Find merge point of two linked list
Assume we have two linked list, we want to find a point in each list, from which all the the nodes share the same value in both list. Then we call this point is "merge point".
In the iamge, the merge point shoud be Node(7).
// Link A length 4, Link B length 5
// All the node after the merge point should be the same
// based on that, we can make sure that the merge point should
// happens in range [x, c] or [y, c], it is not possible to happen at z
// A x - x - c - c
// B z - y - y - c - c
function Node(val) {
return {
val,
next: null
};
} function Link() {
return {
head: null,
tail: null,
length: ,
add(val) {
const newNode = new Node(val); if (this.length === ) {
this.head = newNode;
this.tail = newNode;
this.tail.next = null;
} else {
let temp = this.tail;
this.tail = newNode;
temp.next = this.tail;
} this.length++;
}
};
} // O ( m + n ), Space: O(1)
function findMergePoint(a, b) {
// Link A length 4, Link B length 5
// All the node after the merge point should be the same
// based on that, we can make sure that the merge point should
// happens in range [x, c] or [y, c], it is not possible to happen at z
// A x - x - c - c
// B z - y - y - c - c
let diff, headA = a.head, headB = b.head;
if (a.length > b.length) { // O(1)
diff = a.length - b.length;
[a, b] = [b, a];
} else {
diff = b.length - a.length;
} // reach +diff step for longer
for (let i = ; i < diff; i++) {
headB = headB.next; // O(m)
} while (headA != null && headB.val != null ) { // O(n)
if (headA.val === headB.val) {
return headA;
} headA = headA.next;
headB = headB.next;
} return null;
} const lA = new Link();
lA.add();
lA.add();
lA.add();
lA.add(); const lB = new Link();
lB.add();
lB.add();
lB.add();
lB.add();
lB.add(); console.log(findMergePoint(lA, lB)); // Object {val: 7, next: Object}
[Algorithm] Find merge point of two linked list的更多相关文章
- STL algorithm算法merge(34)
merge原型: std::merge default (1) template <class InputIterator1, class InputIterator2, class Outpu ...
- [Algorithm] 6. Merge Two Sorted Arrays
Description Merge two given sorted integer array A and B into a new sorted integer array. Example A= ...
- [Algorithm] 21. Merge Two Sorted Lists
Merge two sorted linked lists and return it as a new list. The new list should be made by splicing t ...
- [Algorithm] 617. Merge Two Binary Trees
Given two binary trees and imagine that when you put one of them to cover the other, some nodes of t ...
- [LintCode] Merge Two Sorted Lists 混合插入有序链表
Merge two sorted (ascending) linked lists and return it as a new sorted list. The new sorted list sh ...
- STL源代码分析——STL算法merge合并算法
前言 因为在前文的<STL算法剖析>中.源代码剖析许多.不方便学习.也不方便以后复习,这里把这些算法进行归类.对他们单独的源代码剖析进行解说.本文介绍的STL算法中的merge合并算法. ...
- 165. Merge Two Sorted Lists【LintCode by java】
Description Merge two sorted (ascending) linked lists and return it as a new sorted list. The new so ...
- LintCode - Merge Two Sorted List
LintCode - Merge Two Sorted Lists LintCode - Merge Two Sorted Lists Web Link Description Code - C Ti ...
- leetcode 【 Merge k Sorted Lists 】python 实现
题目: Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexit ...
随机推荐
- Creating popup windows in XBAP applications
A colleague at DevelopMentor recently asked me about creating popup windows in XAML browser applicat ...
- X.509 数字证书结构和实例
http://www.cppblog.com/sleepwom/archive/2010/07/08/119746.html 一. X.509数字证书的编码 X.509证书的结构是用ASN1(Abst ...
- Delphi XE5 android 获取网络状态《转》
unit Androidapi.JNI.Network; interface function IsConnected: Boolean; function IsWiFiConnected: Bool ...
- 5日均线MACD
1.5日均线: 5日均线是股市术语,就是股票5天的成交价格或指数的平均值,所对应的是股价的5日均线和指数的5日均线(5MA).均线指标实际上是移动平均线指标的简称. 一般在K 线图中会有3 条或4 条 ...
- 三个实例演示 Java Thread Dump 日志分析
原文地址: http://www.cnblogs.com/zhengyun_ustc/archive/2013/01/06/dumpanalysis.html jstack Dump 日志文件中的线程 ...
- iOS内存管理 -讲的不错,角度独特
ios的内存管理,包括对象的所有权与引用计数.自动释放.访问器方法与属性.一些会改变引用计数的特殊情况 ----- 对象所有权(ownership) 与引用计数 (retain co ...
- iOS开发25:使用SOAP访问Web服务
SOAP是简单对象访问协议,它可看成是HTTP与XML的结合,其中XML部分是作为HTTP报文的实体主体部分.具体信息可以参考百度百科. 在iOS中使用SOAP,需要我们自己组装XML格式的字符串,当 ...
- Auto Layout on iOS Versions prior to 6.0
使用XCODE5.0,出现这个小错误... 解决办法: 选中你的XIB或storyboard,如下图 再查看右边属性栏 去掉最下边的Use Autolayout ,完成. 转:http://blog. ...
- cocos2d-x 真正的定时器之schedule
转载请注明,原文地址:http://blog.csdn.net/musicvs/article/details/8551066 正文: 1. 不调用update函数,调用自己的函数 其实原理是一样的, ...
- U转串口驱动安装
在装有Win7 32位系统的台式机上 先卸载旧驱动,再又一次安装. 对设备管理器里的U转串口设备从本地更新驱动.选择下图文件 系统弹出红色提示框(是否安装XXXX驱动),选择安装,随后该设备由无法启动 ...