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的更多相关文章

  1. STL algorithm算法merge(34)

    merge原型: std::merge default (1) template <class InputIterator1, class InputIterator2, class Outpu ...

  2. [Algorithm] 6. Merge Two Sorted Arrays

    Description Merge two given sorted integer array A and B into a new sorted integer array. Example A= ...

  3. [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 ...

  4. [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 ...

  5. [LintCode] Merge Two Sorted Lists 混合插入有序链表

    Merge two sorted (ascending) linked lists and return it as a new sorted list. The new sorted list sh ...

  6. STL源代码分析——STL算法merge合并算法

    前言 因为在前文的<STL算法剖析>中.源代码剖析许多.不方便学习.也不方便以后复习,这里把这些算法进行归类.对他们单独的源代码剖析进行解说.本文介绍的STL算法中的merge合并算法. ...

  7. 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 ...

  8. LintCode - Merge Two Sorted List

    LintCode - Merge Two Sorted Lists LintCode - Merge Two Sorted Lists Web Link Description Code - C Ti ...

  9. leetcode 【 Merge k Sorted Lists 】python 实现

    题目: Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexit ...

随机推荐

  1. NSLineBreakMode 的区别

    typedef enum {     UILineBreakModeWordWrap = 0,     UILineBreakModeCharacterWrap,     UILineBreakMod ...

  2. .Net 垃圾回收和大对象处理 内存碎片整理

    CLR垃圾回收器根据所占空间大小划分对象.大对象和小对象的处理方式有很大区别.比如内存碎片整理 —— 在内存中移动大对象的成本是昂贵的,让我们研究一下垃圾回收器是如何处理大对象的,大对象对程序性能有哪 ...

  3. C++关键字之virtual

    from://http://blog.csdn.net/xuyuanfan/article/details/9935533 在C++中是没有接口的,要真正实现java中的interface功能,需要使 ...

  4. Linux学习6-CentOS搭建appium服务

    前言 用过appium的应该清楚,每次都需要先启动appium服务,然后再运行代码非常不方便,像selenium就不用启动服务,直接运行脚本. appium实际上只是提供服务,所以我想把它搭建到阿里云 ...

  5. SharePoint 修改项目的new图标显示天数

    前言 最近有这么个需求,用户需要修改新建项目前面的new图标的显示天数,查了很久,发现有powershell命令或者stsadm命令可以,分享给大家. PowerShell命令 $wa = Get-S ...

  6. Build Web Apps in Node and Express视频下载

    上传到百度云了,点击这里下载>>    作者使用的是Mac系统,不过Windows也差不多,主要理解express一些基本配置和使用,讲的比较基础,希望对node.js.express有兴 ...

  7. H2:开源内存数据库引擎

    本资源由 伯乐在线 - 刘立华 整理 H2是一个开源的内存数据库.Java编写.快速.小巧(1.5MB jar包)还提供了Web控制台管理数据库内容. 主要功能 非常快速的数据库引擎. 开源. Jav ...

  8. Java 文件路径相关

    不得不说Java的文件路径弄得很复杂, 有编译目录和resource目录什么的和解释型语言(PHP)的就是不一样 搞了好几年java一直没认真去研究这些个破路径怎么回事, 每次都忘记, 梳理一下备忘 ...

  9. CF 329C(Graph Reconstruction-随机化求解-random_shuffle(a+1,a+1+n))

    C. Graph Reconstruction time limit per test 3 seconds memory limit per test 256 megabytes input stan ...

  10. 如何在CentOS 7.2上创建NFS的Share,然后让Client可以访问

    讲得详细清楚明白的好文. Setting Up an NFS Server and Client on CentOS 7.2 https://www.howtoforge.com/tutorial/s ...