两个链接合并

了解问题 (Understand the Problem)

We are given two singly linked lists and we have to find the point at which they merge.

我们给了两个单链表,我们必须找到它们合并的点。

[SLL 1] 1--->3--->5                     \                      9--->12--->17--->None                     /     [SLL 2] 7--->8

The diagram above illustrates that the merge occurs at node 9.

上图说明了合并发生在节点9上。

We can rest assured that the parameters we are given, head1 and head2, which are the heads of both lists will never be equal and will never be none. The two lists are also guaranteed to merge at some point.

我们可以放心,我们给定的参数head1和head2都是两个列表的头部,它们永远不会相等,也永远不会相等。 这两个列表也一定会合并。

We need a plan to find and return the integer data value of the node where the two lists merge.

我们需要一个计划来查找并返回两个列表合并的节点的整数数据值。

计划 (Plan)

In order to traverse through the lists to find the point at which they merge, we need to set two different pointers. One for the first singly linked list, another for the second. Remember that we are given the heads of both as parameters, so we will set our pointers to them in order to start from the beginning of each.

为了遍历列表以查找它们合并的点,我们需要设置两个不同的指针。 一个用于第一个单链表,另一个用于第二个链表。 请记住,我们将两个的头都作为参数,因此我们将设置指向它们的指针,以便从每个头开始。

pointer1 = head1pointer2 = head2

To begin traversing the lists, we’ll need create a while loop to loop through the lists while the lists are not None.

要开始遍历列表,我们需要创建一个while循环来遍历列表,而列表不是None。

while not None:

If at any point, pointer1 and pointer2 are equal, we must break out of the while loop, as we have found the node where the two lists merge.

如果在任何时候,pointer1和pointer2相等,则必须打破while循环,因为我们找到了两个列表合并的节点。

if pointer1 == pointer2:    break

However, if it is not equal, we will move forward by utilizing .next.

但是,如果不相等,我们将利用.next向前移动。

pointer1 = pointer1.nextpointer2 = pointer2.next

As we can see from the example diagram, our first singly linked list, SLL 1, is shorter than SLL 2. So let’s suppose SLL 1 hits None before SLL 2 finds the merge node. In this case we will simply begin again, setting the same if statement for SLL 2, in case we have a test case in our program where the second SLL is the shorter one.

从示例图中可以看到,我们的第一个单链列表SLL 1比SLL 2短。因此,假设SLL 1在SLL 2找到合并节点之前命中None。 在这种情况下,我们将再次开始,为SLL 2设置相同的if语句,以防程序中有一个测试用例,其中第二个SLL是较短的。

if pointer1 == None:    pointer1 == head1if pointer2 == None:    pointer2 == head1

This logic of starting over will repeat in the while loop until both pointers find the merging node at the same time, or in other words, until pointer1 and pointer2 are equal. When that happens, we must remember to do what was asked of us and return the integer data value of that node.

重新开始的逻辑将在while循环中重复,直到两个指针同时找到合并节点,或者换句话说,直到指针1和指针2相等为止。 发生这种情况时,我们必须记住执行要求我们执行的操作,并返回该节点的整数数据值。

return pointer1.data

Because this will also be pointer2’s data, we can return this in lieu of pointer1. It has the same value.

因为这也将是指针2的数据,所以我们可以代替指针1返回它。 它具有相同的值。

return pointer2.data 

执行 (Execute)

# For our reference:## SinglyLinkedListNode:#     int data#     SinglyLinkedListNode next##def findMergeNode(head1, head2):    # Set the pointers    pointer1 = head1    pointer2 = head2    while not None:        # if we found the merging node, then break out of the loop        if pointer1 == pointer2:            break        #traverse through lists        pointer1 = pointer1.next        pointer2= pointer2.next        # Begin again if one was shorter than the other        if pointer1 == None:            pointer1 = head1        if pointer2 == None:            pointer2 = head2    return pointer1.data

升级编码 (Level Up Coding)

Thanks for being a part of our community! Subscribe to our YouTube channel or join the Skilled.dev coding interview course.

感谢您加入我们的社区! 订阅我们的YouTube频道或参加Skilled.dev编码面试课程

翻译自: https://levelup.gitconnected.com/how-to-find-the-merge-point-of-two-linked-lists-ba55a129caa2

两个链接合并


http://www.taodudu.cc/news/show-997612.html

相关文章:

  • 工程师的成熟模型_数据工程师的成熟度
  • scrape创建_确实在2分钟内对Scrape公司进行了评论和评分
  • 如何不认识自己
  • plotly python_使用Plotly for Python时的基本思路
  • java项目经验行业_行业研究以及如何炫耀您的项目
  • 数据科学 python_适用于数据科学的Python vs(和)R
  • r怎么对两组数据统计检验_数据科学中最常用的统计检验是什么
  • 深度学习概述_深度感测框架概述
  • 为什么即使在班级均衡的情况下,准确度仍然令人困扰
  • 接受拒绝算法_通过算法拒绝大学学位
  • 为什么用scrum_为什么Scrum糟糕于数据科学
  • 使用集合映射和关联关系映射_使用R进行基因ID映射
  • 详尽kmp_详尽的分步指南,用于数据准备
  • SMSSMS垃圾邮件检测器的专业攻击
  • 使用Python进行地理编码和反向地理编码
  • grafana 创建仪表盘_创建仪表盘前要问的三个问题
  • 大数据对社交媒体的影响_数据如何影响媒体,广告和娱乐职业
  • python 装饰器装饰类_5分钟的Python装饰器指南
  • 机器学习实际应用_机器学习的实际好处是什么?
  • mysql 时间推移_随着时间的推移可视化COVID-19新案例
  • 海量数据寻找最频繁的数据_寻找数据科学家的“原因”
  • kaggle比赛数据_表格数据二进制分类:来自5个Kaggle比赛的所有技巧和窍门
  • netflix_Netflix的Polynote
  • 气流与路易吉,阿戈,MLFlow,KubeFlow
  • 顶级数据恢复_顶级R数据科学图书馆
  • 大数据 notebook_Dockerless Notebook:数据科学期待已久的未来
  • 微软大数据_我对Microsoft的数据科学采访
  • 如何击败腾讯_击败股市
  • 如何将Jupyter Notebook连接到远程Spark集群并每天运行Spark作业?
  • twitter 数据集处理_Twitter数据清理和数据科学预处理

两个链接合并_如何找到两个链接列表的合并点相关推荐

  1. c++两个vector合并_这才是真正的 Git——分支合并

    本文作者:lzaneli,腾讯 TEG 前端开发工程师 "合并前文件还在的,合并后就不见了"."我遇到 Git 合并的 bug 了" 是两句经常听到的话,但真的 ...

  2. 如何将两个集合合并_如何将剪切的音频文件进行合并

    下面就和大家一起来学习如何将音频文件剪切为多个片段再重新合并在一起.剪辑所用的音频转换器有很多,但是得找到一款合适自己的,下面小编就来为大家讲解一种方法. 使用工具: 音频转换器https://www ...

  3. python合并两个excel文件_利用Python将多个excel文件合并为一个文件

    # -*- coding: utf-8 -*- #导入需要使用的包 import xlrd  #读取Excel文件的包 import xlsxwriter   #将文件写入Excel的包 #打开一个e ...

  4. 两个list关联合并_算法分享---两个有序链表的合并(C语言)

    定义两个指针,分别指向两个链表的头结点,在两个链表都不为空时, 比较两个链表结点的值的大小. 若链表l1的节点大于l2的节点,则将l1的节点值赋值给l3.l1的指针指向下一个节点. 反之,则则将l2的 ...

  5. 两个音轨合并_怎样把两个音频文件合并成一个?

    展开全部 由于音频文件62616964757a686964616fe4b893e5b19e31333431353935太大等原因,有时候在网上下载音频文件,下载完成后发现文件被分成了很多个小片段,这样 ...

  6. 一个路由器两个网段互通_如何判断两个IP地址是否在同一个网段?什么是子网掩码?...

    前几天咱们了解:三种方法告诉你项目超过255个摄像机怎么设置IP?什么是公网ip?什么又是内网ip?为什么ip地址通常以192.168开头? 也学习了:二.三层交换机与路由器的区别! 但是有好多人对I ...

  7. python两个元组相加_《第5章 Python 列表与元组》5.1.3 序列相加(Adding)!

    <高中信息技术 Python编程> 教学案<第5章 Python 列表与元组> 5.1.3 序列相加(Adding)! 06:15 1 #使用加号可以进行序列的连接操作,输入如 ...

  8. JAVA中两台电脑通信_如何实现两台PC终端基于MAC地址互相通信

    /*** 一.首先介绍下license授权机制的原理:1. 生成密钥对,方法有很多.2. 授权者保留私钥,使用私钥对包含授权信息(如使用截止日期,MAC地址等)的license进行数字签名.3. 公钥 ...

  9. mysql对比两个字段差异_如何对比两个表字段差异

    做这个项目,以前只在业务表增加字段,而没在其历史表增加对应字段 最近客户需要将业务表和历史表的字段保持一致.于是用这个语句来进行对比即可,如果查出来有数据,则表示有字段差异 --2个表比较 DECLA ...

最新文章

  1. Office PPT如何切换到返回幻灯片
  2. Java知识点详解 4 泛型
  3. oracle控制文件加载数据,关于SQLLOAD控制文件参数的问题
  4. DataTable克隆行
  5. 实现列表CListCtrl可点击编辑
  6. Kafka 中 partition replica 默认分配机制
  7. KEIL软件安装教程
  8. 9个免费可商用的字体推荐
  9. 百度地图怎么不显示服务器地址,百度地图使用指南
  10. 梦幻西游这么火, Python也不甘示弱, Python写游戏辅助软件!
  11. vue中rule数据校验
  12. Win10重装系统/迁移系统,教你如何简单快速删除原系统文件
  13. 手把手教你进行微信小程序开发案例1---计算器
  14. Error while building/deploying project QTtest (kit: Desktop Qt 5.9.0 MinGW 32bit)When executing~
  15. PS色彩算法理解记录 4 Screen
  16. 古典概率,条件概率,全概率
  17. Go基础编程:HTTP报文浅析
  18. Sense校准移动到设置中
  19. 最大熵(max entropy)模型原理小结
  20. 模型model的增删查改操作

热门文章

  1. CRM公海自动回收规则
  2. NLPPython笔记——WordNet
  3. bzoj 4552: [Tjoi2016Heoi2016]排序
  4. 第1次作业:阅读优秀博文谈感想
  5. 感想篇:4)越来越精简的机械设计
  6. lua math.random()
  7. QTP基础学习(二)启动与设置
  8. CentOS5.6环境安装oracle 10g(完整版)
  9. C#操作静态路由表(增、删、改、查、遍历)
  10. Anaconda安装tensorflow报错问题解决方法