摘要:題目要求假設(shè)存在這樣一個(gè)鏈表,在鏈表的每一個(gè)節(jié)點(diǎn)中,除了記錄了自身值和指向下一個(gè)節(jié)點(diǎn)的指針,還有一個(gè)隨機(jī)指針指向鏈表中任意一個(gè)節(jié)點(diǎn)。所以可以在兩次遍歷后完成任務(wù)。最后一圈遍歷,我們調(diào)整指針,恢復(fù)原鏈表和塑造新鏈表。
題目要求
A linked list is given such that each node contains an additional random pointer which could point to any node in the list or null. Return a deep copy of the list.
假設(shè)存在這樣一個(gè)鏈表,在鏈表的每一個(gè)節(jié)點(diǎn)中,除了記錄了自身值和指向下一個(gè)節(jié)點(diǎn)的指針,還有一個(gè)隨機(jī)指針指向鏈表中任意一個(gè)節(jié)點(diǎn)?,F(xiàn)在要求深度復(fù)制這份鏈表,在不改變原鏈表內(nèi)容的情況下,創(chuàng)建一組新的對象,該組對象中的值和原鏈表中的值相同。
思路一:map因?yàn)槿我夤?jié)點(diǎn)指針的值在一遍遍歷中是無法復(fù)制的。所以我們可以基本確定至少需要兩次遍歷才能夠充分的復(fù)制鏈表。那么在第一次遍歷的時(shí)候,我們采用什么樣的數(shù)據(jù)結(jié)構(gòu)來記錄第一次遍歷的值呢?最直觀的思路是使用map,第一次遍歷時(shí)我們訪問next指針,將當(dāng)前對象和當(dāng)前對象的復(fù)制對象作為key-value值存入map中。第二次遍歷時(shí),我們可以一邊調(diào)整復(fù)制對象的next指針,一邊調(diào)整復(fù)制對象的random指針,這兩個(gè)指針指向的都將是map中源對象的復(fù)制對象。所以可以在兩次遍歷后完成任務(wù)。
public RandomListNode copyRandomList(RandomListNode head) { if(head==null) return null; Map思路二:利用鏈表的特性map = new HashMap (); RandomListNode temp = head; while(temp!=null){ map.put(temp, new RandomListNode(temp.label)); temp = temp.next; } temp = head; while(temp!=null){ RandomListNode cur = map.get(temp); cur.next = map.get(temp.next); cur.random = map.get(temp.random); temp = temp.next; } return map.get(head); }
那么我們有沒有可能在不使用map的情況下通過舊節(jié)點(diǎn)實(shí)現(xiàn)對新節(jié)點(diǎn)的檢索?答案是肯定的。我們可以充分利用鏈表的特性來保存對新的復(fù)制節(jié)點(diǎn)的指針。第一圈遍歷時(shí),我們可以將復(fù)制的節(jié)點(diǎn)插入至被復(fù)制的節(jié)點(diǎn)的后面。這樣我們就可以通過舊節(jié)點(diǎn)的next值找到新節(jié)點(diǎn)。在第二圈遍歷的時(shí)候,我們可以依次更新復(fù)制節(jié)點(diǎn)的random指針,將其指向新的復(fù)制節(jié)點(diǎn)。最后一圈遍歷,我們調(diào)整next指針,恢復(fù)原鏈表和塑造新鏈表。
public RandomListNode copyRandomList2(RandomListNode head) { if(head==null) return null; RandomListNode current = head, copyHead = null, copyCurrent = null; while(current!=null){ RandomListNode temp = new RandomListNode(current.label); temp.next = current.next; current.next = temp; current = current.next.next; } current = head; while(current!=null){ if(current.random!=null){ current.next.random = current.random.next; } current = current.next.next; } current = head; copyCurrent = copyHead = head.next; head.next = head.next.next; while(current != null){ current.next = current.next.next; copyCurrent.next = copyCurrent.next.next; copyCurrent = copyCurrent.next; current = current.next; } return copyHead; }
想要了解更多開發(fā)技術(shù),面試教程以及互聯(lián)網(wǎng)公司內(nèi)推,歡迎關(guān)注我的微信公眾號!將會不定期的發(fā)放福利哦~
文章版權(quán)歸作者所有,未經(jīng)允許請勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。
轉(zhuǎn)載請注明本文地址:http://m.hztianpu.com/yun/70454.html
LeetCode[138] Copy List with Random Pointer A linked list is given such that each node contains an additional random pointer which could point to any node in the list or null. Return a deep copy of t...
摘要:給定一個(gè)鏈表,每個(gè)節(jié)點(diǎn)包含一個(gè)額外增加的隨機(jī)指針,該指針可以指向鏈表中的任何節(jié)點(diǎn)或空節(jié)點(diǎn)。要求返回這個(gè)鏈表的深拷貝。提示你必須返回給定頭的拷貝作為對克隆列表的引用。確定隨機(jī)節(jié)點(diǎn)的關(guān)系之后再拆分鏈表。其時(shí)間復(fù)雜度為,空間復(fù)雜度為。 給定一個(gè)鏈表,每個(gè)節(jié)點(diǎn)包含一個(gè)額外增加的隨機(jī)指針,該指針可以指向鏈表中的任何節(jié)點(diǎn)或空節(jié)點(diǎn)。 要求返回這個(gè)鏈表的深拷貝。 A linked list is g...
摘要:給定一個(gè)鏈表,每個(gè)節(jié)點(diǎn)包含一個(gè)額外增加的隨機(jī)指針,該指針可以指向鏈表中的任何節(jié)點(diǎn)或空節(jié)點(diǎn)。要求返回這個(gè)鏈表的深拷貝。提示你必須返回給定頭的拷貝作為對克隆列表的引用。確定隨機(jī)節(jié)點(diǎn)的關(guān)系之后再拆分鏈表。其時(shí)間復(fù)雜度為,空間復(fù)雜度為。 給定一個(gè)鏈表,每個(gè)節(jié)點(diǎn)包含一個(gè)額外增加的隨機(jī)指針,該指針可以指向鏈表中的任何節(jié)點(diǎn)或空節(jié)點(diǎn)。 要求返回這個(gè)鏈表的深拷貝。 A linked list is g...
摘要:棧迭代復(fù)雜度時(shí)間空間如果不算新鏈表的空間則是思路由于隨機(jī)指針有可能產(chǎn)生環(huán)路,我們不能直接沿著隨機(jī)指針的方向一個(gè)一個(gè)復(fù)制。同時(shí)我們又不能沿著指針直接復(fù)制,因?yàn)槲覀儾恢离S機(jī)指針?biāo)赶虻男鹿?jié)點(diǎn)是哪個(gè)。 Copy List with Random Pointer A linked list is given such that each node contains an additiona...
摘要:大體意思就是,先復(fù)制到,順便將所有的放在再復(fù)制所有的到,順便將所有的放在最后令,令,將和分離,返回的頭結(jié)點(diǎn) Problem A linked list is given such that each node contains an additional random pointer which could point to any node in the list or null. ...
閱讀 9910·2021-11-18 10:02
閱讀 2758·2019-08-30 15:43
閱讀 2738·2019-08-30 13:50
閱讀 1494·2019-08-30 11:20
閱讀 2786·2019-08-29 15:03
閱讀 3717·2019-08-29 12:36
閱讀 992·2019-08-23 17:04
閱讀 694·2019-08-23 14:18