两个链接合并_如何找到两个链接列表的合并点
兩個(gè)鏈接合并
了解問(wèn)題 (Understand the Problem)
We are given two singly linked lists and we have to find the point at which they merge.
我們給了兩個(gè)單鏈表,我們必須找到它們合并的點(diǎn)。
[SLL 1] 1--->3--->5\
9--->12--->17--->None
/
[SLL 2] 7--->8
The diagram above illustrates that the merge occurs at node 9.
上圖說(shuō)明了合并發(fā)生在節(jié)點(diǎn)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.
我們可以放心,我們給定的參數(shù)head1和head2都是兩個(gè)列表的頭部,它們永遠(yuǎn)不會(huì)相等,也永遠(yuǎn)不會(huì)相等。 這兩個(gè)列表也一定會(huì)合并。
We need a plan to find and return the integer data value of the node where the two lists merge.
我們需要一個(gè)計(jì)劃來(lái)查找并返回兩個(gè)列表合并的節(jié)點(diǎn)的整數(shù)數(shù)據(jù)值。
計(jì)劃 (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.
為了遍歷列表以查找它們合并的點(diǎn),我們需要設(shè)置兩個(gè)不同的指針。 一個(gè)用于第一個(gè)單鏈表,另一個(gè)用于第二個(gè)鏈表。 請(qǐng)記住,我們將兩個(gè)的頭都作為參數(shù),因此我們將設(shè)置指向它們的指針,以便從每個(gè)頭開始。
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.
要開始遍歷列表,我們需要?jiǎng)?chuàng)建一個(gè)while循環(huán)來(lái)遍歷列表,而列表不是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.
如果在任何時(shí)候,pointer1和pointer2相等,則必須打破while循環(huán),因?yàn)槲覀冋业搅藘蓚€(gè)列表合并的節(jié)點(diǎn)。
if pointer1 == pointer2:break
However, if it is not equal, we will move forward by utilizing .next.
但是,如果不相等,我們將利用.next向前移動(dòng)。
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.
從示例圖中可以看到,我們的第一個(gè)單鏈列表SLL 1比SLL 2短。因此,假設(shè)SLL 1在SLL 2找到合并節(jié)點(diǎn)之前命中None。 在這種情況下,我們將再次開始,為SLL 2設(shè)置相同的if語(yǔ)句,以防程序中有一個(gè)測(cè)試用例,其中第二個(gè)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循環(huán)中重復(fù),直到兩個(gè)指針同時(shí)找到合并節(jié)點(diǎn),或者換句話說(shuō),直到指針1和指針2相等為止。 發(fā)生這種情況時(shí),我們必須記住執(zhí)行要求我們執(zhí)行的操作,并返回該節(jié)點(diǎn)的整數(shù)數(shù)據(jù)值。
return pointer1.dataBecause this will also be pointer2’s data, we can return this in lieu of pointer1. It has the same value.
因?yàn)檫@也將是指針2的數(shù)據(jù),所以我們可以代替指針1返回它。 它具有相同的值。
return pointer2.data執(zhí)行 (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
升級(jí)編碼 (Level Up Coding)
Thanks for being a part of our community! Subscribe to our YouTube channel or join the Skilled.dev coding interview course.
感謝您加入我們的社區(qū)! 訂閱我們的YouTube頻道或參加Skilled.dev編碼面試課程 。
翻譯自: https://levelup.gitconnected.com/how-to-find-the-merge-point-of-two-linked-lists-ba55a129caa2
兩個(gè)鏈接合并
總結(jié)
以上是生活随笔為你收集整理的两个链接合并_如何找到两个链接列表的合并点的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 做梦梦到下大雨发大水什么意思
- 下一篇: 工程师的成熟模型_数据工程师的成熟度