[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 ...
随机推荐
- 在Asp.net core中使用WebScocket
今天小试了一下在Asp.net core中使用websocket,这里记录一下: 在 Startup 类的 Configure 方法中添加 WebSocket 中间件. app.UseWebSocke ...
- LPC43xx OTP
- 关于STM32数据手册中的定时器信号
首先,我们可以看到这个图大概有两个不分,一个部分是时钟源,另一个部分则是输入输出 时钟源计数,到CNT计数器,然后根据捕获比较寄存器进行记录或比较.记录或比较有不同的配置. 首先是TI信号TI1 TI ...
- Chilkat----开源站点之VS2010 CKMailMan一个很好的邮件发送开源开发包
Chilkat 是一个很好的开源站点,有各种开源库. 开发语言主要有Classic ASP •C • C++ • C# • Delphi ActiveX • Delphi DLL • Visual F ...
- 查看内核页表kernel_page_tables (aarch32)
作者 彭东林 pengdonglin137@163.com 平台 Linux-4.10.17 Qemu + vexpress-ca9 概述 通过配置内核,会在/sys/kernel/deb ...
- 咏南中间件支持TMS WEB CORE客户端
咏南中间件支持TMS WEB CORE客户端 TMS WEB CORE是优秀的JS前端,搭配咏南中间件后端,可以进行快速的企业应用开发.
- ArcGIS Engine Maplex Label(标注)使用一例(转)
/// <summary> /// MaplexEngine标注 /// </summary> /// <param name="pGeoFeatLyr&quo ...
- FEC详解三
转自:http://blog.csdn.net/Stone_OverLooking/article/details/77752076 继续上文讲解: 3) 标准的RTP头结构如下所示: 其中第一个字节 ...
- Java判断多个时间段是否重叠(重叠区间个数)
import java.util.ArrayList; import java.util.Collections; import java.util.List; /** * 判断多个时间段是否出现重叠 ...
- Weblogic跨域session冲突解决办法
一.现象: 在WebLogic中,有两个不同域A(端口:9000)和B(端口:8000),应用CA在域A中,应用CB在域B中,进行如下操作: 1.先登录应用CA,再登录应用CB,然后,切换回应用CA, ...