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

    StatCounter provides free customisable hit counters, visitor tracking, web analytics and website sta ...

  2. C#中使用throw和throw ex抛出异常的区别

    通常,我们使用try/catch/finally语句块来捕获异常,就像在这里说的.在抛出异常的时候,使用throw和throw ex有什么区别呢? 假设,按如下的方式调用几个方法: →在Main方法中 ...

  3. ArcEngine设置有牵引线的标注

    来自:https://blog.csdn.net/u011609113/article/details/51372827/ 在ArcGIs中很容易就能设置带有牵引线的标注.   在ArcEngine中 ...

  4. java.security.InvalidKeyException: Illegal key size aes解密失败

    使用微信时定期提示:java.security.InvalidKeyException: Illegal key size和 com.qq.weixin.mp.aes.AesException: ae ...

  5. SharePoint 2016 工作流报错“未安装应用程序管理共享服务代理”

    前言 最近为SharePoint 2016环境,配置了状态机工作流,然后,用spd创建的时候可以保存,但是发布的时候报错,经过排查解决了问题,记录一下. 报错截图 下面是SharePoint Desi ...

  6. gzip格式解压缩

    gzip格式解压缩 有时候网络请求中会出现gzip格式的数据,而我们无法通过常规办法进行解析: 这时候可以使用下面的这个工具来解决这个问题: https://github.com/mattt/Godz ...

  7. Java并发编程的艺术(三)——volatile

    1. 并发编程的两个关键问题 并发是让多个线程同时执行,若线程之间是独立的,那并发实现起来很简单,各自执行各自的就行:但往往多条线程之间需要共享数据,此时在并发编程过程中就不可避免要考虑两个问题:通信 ...

  8. 通过xml文件来设置动画

    @android:anim/accelerate_interpolator: 越来越快 @android:anim/decelerate_interpolator:越来越慢 @android:anim ...

  9. Java获取当前时间30天之前的时间

    //方法一 SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); String maxDateStr = " ...

  10. HashMap 与 ConcurrentHashMap

    1. HashMap 1) 并发问题 HashMap的并发问题源于多线程访问HashMap时, 如果存在修改Map的结构的操作(增删, 不包括修改), 则有可能会发生并发问题, 表现就是get()操作 ...