兩個鏈接合并_如何找到兩個鏈接列表的合并點

兩個鏈接合并

了解問題 (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.pswp.cn/news/392398.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/392398.shtml
英文地址,請注明出處:http://en.pswp.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 雖然谷…