两个链接合并_如何找到两个链接列表的合并点

两个链接合并

了解问题 (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.

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

Image for post
pointer1 = head1
pointer2 = 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向前移动。

Image for post
pointer1 = pointer1.next
pointer2 = 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 == head1
if 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.mzph.cn/news/392398.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

安装veket到移动硬盘NTFS分区

如果你已经看过《手动安装veket到硬盘》和《简单的将veket安装到U盘的方法》两篇文章并且安装成功的话,说明不适用本文的安装环境,就不用往下看了。 《手动安装veket到硬盘》一文采用grub4dos来引导硬盘上的veket,主要是用来在本机已安装Wind…

简书使用小技巧

1、不同字体  在 设置->基础设置->富文本 模式下可以实现 2、添加图片,让文章更生动 3、添加代码框 !注意:设置为Markdown模式后,只对新创建的文章起作用。转载于:https://www.cnblogs.com/HMJ-29/p/7049540.html

掩码 项目编码_每天进行20天的编码项目

掩码 项目编码by Angela He通过何安佳 每天进行20天的编码项目 (A coding project a day for 20 days) 我如何在20天内自学Web开发 (How I taught myself web development in 20 days) It was the first day of winter break for Stanford students. Back at home, I opened a…

java循环一年月份天数和_javawhile循环编写输入某年某月某日,判断这一天是这一年的第几…...

该楼层疑似违规已被系统折叠 隐藏此楼查看此楼public class ZuoYe9 {public static void main(String[] args) {int days0; //存储变量这一年的第几天//1.输入年,月,日Scanner inputnew Scanner(System.in);System.out.println("请输入年份&#xf…

leetcode 605. 种花问题(贪心算法)

假设你有一个很长的花坛,一部分地块种植了花,另一部分却没有。可是,花卉不能种植在相邻的地块上,它们会争夺水源,两者都会死去。 给定一个花坛(表示为一个数组包含0和1,其中0表示没种植花&…

工程师的成熟模型_数据工程师的成熟度

工程师的成熟模型数据科学与机器学习 (DATA SCIENCE AND MACHINE LEARNING) What does a data engineer do?数据工程师做什么? Let’s start with three big wars that we need to understand before understanding what a data engineer does.让我们从理解数据工…

杭电2064

此题是一道简单的递归 此题是一道递归运算题,这题又是一道汉诺塔问题!!!只要了解其规律,呵呵,你就可以很快AC了!! 这是一般的汉诺塔问题的解题方法照片!!&…

/ ./ ../ 的区别

/ 根目录 (绝对路径) ./ 当前目录 ../父级目录 (相对路径) ./home是当前目录下的一个叫home的目录/home是绝对路径的/home就是根下的home目录转载于:https://www.cnblogs.com/sjd1118/p/7055475.html

java设置表格列不可修改_Java DefaultTableModel使单元格不可编辑JTable

参见英文答案 >How to make a JTable non-editable 7个我有一个JAVA项目,并希望使用DefaultTableModel使我的JTable不可编辑.我知道一个解决方法,称为:JTable table new JTable(...){public boolean isCellEditable(int row…

阻塞队列实现

⭐ 作者:小胡_不糊涂 🌱 作者主页:小胡_不糊涂的个人主页 📀 收录专栏:JavaEE 💖 持续更文,关注博主少走弯路,谢谢大家支持 💖 阻塞队列 1. 什么是阻塞队列2. 标准库中的…

graphql入门_GraphQL入门指南

graphql入门by Leonardo Maldonado莱昂纳多马尔多纳多(Leonardo Maldonado) GraphQL入门指南 (A Beginner’s Guide to GraphQL) One of the most commonly discussed terms today is the API. A lot of people don’t know exactly what an API is. Basically, API stands fo…

leetcode 239. 滑动窗口最大值(单调队列)

给你一个整数数组 nums,有一个大小为 k 的滑动窗口从数组的最左侧移动到数组的最右侧。你只可以看到在滑动窗口内的 k 个数字。滑动窗口每次只向右移动一位。 返回滑动窗口中的最大值。 示例 1: 输入:nums [1,3,-1,-3,5,3,6,7], k 3 输出…

scrape创建_确实在2分钟内对Scrape公司进行了评论和评分

scrape创建网页搜罗,数据科学 (Web Scraping, Data Science) In this tutorial, I will show you how to perform web scraping using Anaconda Jupyter notebook and the BeautifulSoup library.在本教程中,我将向您展示如何使用Anaconda Jupyter笔记本…

ArcGIS自定义高程

没写呢。 转载于:https://www.cnblogs.com/jiangyuanjia/p/11220183.html

Java基础——String类(一)

一、String 类代表字符串 Java 程序中的所有字符串字面值(如 "abc" )都作为此类的实例实现。 字符串是常量;它们的值在创建之后不能更改。字符串缓冲区支持可变的字符串。因为 String 对象是不可变的,所以可以共享。例如…

java jol原理_Java对象布局(JOL)实现过程解析

java对象布局JOL(java object layout),描述对象在堆内存的布局。如下图:1.markword 固定长度8byte,描述对象的identityhashcode,分代年龄,锁信息等(https://www.jb51.net/article/183984.htm);2.klasspoint 固定长度4b…

数据库维护相关

(1)SQL Server 查看数据表使用空间 exec sp_spaceused 表名 (2)SQL Server 数据表使用空间排序 exec sp_MSForeachTable precommandN create table ##( table_name sysname, records int, save_space Nvarchar(10), use_space var…

Redux初学者指南

by Safeer Hayat通过更安全的哈亚特 Understanding Redux as a beginner can be quite confusing. Redux has an abundance of new terms and concepts which are often pretty unintuitive. This guide presents a very simplified example of a Redux implementation. I wil…

leetcode 86. 分隔链表(链表)

给你一个链表和一个特定值 x ,请你对链表进行分隔,使得所有小于 x 的节点都出现在大于或等于 x 的节点之前。 你应当保留两个分区中每个节点的初始相对位置。 示例: 输入:head 1->4->3->2->5->2, x 3 输出&am…

极光推送

推送原理 IOS 通过APNs推送服务。 每个设备只要保持一个与APNs的常链接,服务器将要推送的消息发送给APNs,APNs再将消息转发到响应的手机,手机内置的程序再进行分发,到响应的APP,就能很好的实现推送功能 Andriod 虽然谷…