[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 ...
随机推荐
- LPC43xx SGPIO I2C Implementation
I²C SGPIO Configuration SGPIO is a hardware feature of LPC4300 series. There are 16 SGPIO pins calle ...
- OpenOCD 0.9.0 release
OpenOCD 0.9.0 release May 18th, 2015 I’m happy to announce the release of OpenOCD version 0.9.0, fin ...
- [Go] 跨平台文件系统监控工具 fsnotify 应用举例
项目地址:https://github.com/fsnotify/fsnotify fsnotify 能监控指定文件夹内 文件的修改情况,如 文件的 增加.删除.修改.重命名等操作. 官方给出了以下注 ...
- 应收事物处理删除 SQL 语句
/* Formatted on 2018/3/15 10:07:48 (QP5 v5.256.13226.35538) */ --组织表 SELECT * FROM hr_organization_u ...
- excel 批注
Excel VBA之Name对象.Comment对象及字体设置等,点滴学习积累 存在的方式 百家号11-1518:46 ======================================== ...
- ASP.NET :Virtual Application vs Virtual Directory
原文地址:http://blogs.msdn.com/b/wenlong/archive/2006/11/22/virtual-application-vs-virtual-directory.asp ...
- python测试开发django-36.一对一(OneToOneField)关系查询
前言 前面一篇在xadmin后台一个页面显示2个关联表(OneToOneField)的字段,使用inlines内联显示.本篇继续学习一对一(OneToOneField)关系的查询. 上一篇list_d ...
- PL/SQL 存储过程
PL/SQL复习九 存储过程 无参数的存储过程: create or replace procedure out_time is begin dbms_output.put_line(to_char( ...
- 关于android studio 出现Error:Execution failed for task ':app:preDebugAndroidTestBuild'. 的解决办法
Error:Execution failed for task ':app:preDebugAndroidTestBuild'. > Conflict with dependency 2018年 ...
- ExtJS 4.2 教程-08:布局系统详解
ExtJS 4.2 系列教程导航目录: ExtJS 4.2 教程-01:Hello ExtJS ExtJS 4.2 教程-02:bootstrap.js 工作方式 ExtJS 4.2 教程-03:使用 ...