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. ubuntu修改时区和时间的方法

    改时区参考 http://blog.sina.com.cn/s/blog_6c9d65a1010145st.html 1.首先查看时区: swfsadmin@swfsubuntu:~$ date -R ...

  2. RabbitMQ高级指南:从配置、使用到高可用集群搭建

    本文大纲: 1. RabbitMQ简介 2. RabbitMQ安装与配置 3. C# 如何使用RabbitMQ 4. 几种Exchange模式 5. RPC 远程过程调用 6. RabbitMQ高可用 ...

  3. java.lang.UnsupportedClassVersionError: com/android/dx/command/Main : Unsupported major.minor version 52.0

    严重性 代码 说明 项目 文件 行 禁止显示状态错误 xamarin java.lang.UnsupportedClassVersionError: com/android/dx/command/Ma ...

  4. 【Devops】【Jenkins】Jenkins插件安装失败处理方法

    本篇解释:Jenkins插件安装失败处理方法 不论是刚启动成功后进行的推荐插件安装,还是后期使用Jenkins过程中进行插件的安装.出现插件安装失败的问题,可以通过本篇解决! [注意,插件下载安装失败 ...

  5. 安装express并创建工程

    安装express npm install express-generator -g 使用命令创建express工程, express projectName 生成express相应的工程结构 bin ...

  6. Oracle数据库(一)

    Oracle 是一个数据库管理系统,是Oracle公司的核心产品.其在数据安全性与安整性控制方面的优越性能,以及跨操作系统.跨硬件平台的数据操作能力.基于“客户端/服务    器”(Client/Se ...

  7. 概率校准与Brier分数

    1.再提逻辑回归 前面已经讲过了逻辑回归,这里不再细讲,只是简单的说一个函数,主要是方便大家更好的理解概率校准. 在逻辑回归中,用的最多的就是sigmod函数,这个函数的作用就是把无限大或者无限小的数 ...

  8. 【翻译自mos文章】Windows平台下的 Oraagent Memory Leak

    来源于: Oraagent Memory Leak (文档 ID 1956840.1) APPLIES TO: Oracle Database - Enterprise Edition - Versi ...

  9. Team Foundation(通常记作“TFS”)

    ylbtech-Miscellaneos:Team Foundation(通常记作“TFS”) Team Foundation是一个服务平台,为 Microsoft 提供源代码管理数据收集报告等工作. ...

  10. 奇怪吸引子---AnishchenkoAstakhov

    奇怪吸引子是混沌学的重要组成理论,用于演化过程的终极状态,具有如下特征:终极性.稳定性.吸引性.吸引子是一个数学概念,描写运动的收敛类型.它是指这样的一个集合,当时间趋于无穷大时,在任何一个有界集上出 ...