java算法題解LeetCode35復(fù)雜鏈表的復(fù)制實(shí)例
題目
AC 劍指 Offer 35. 復(fù)雜鏈表的復(fù)制請(qǐng)實(shí)現(xiàn) copyRandomList 函數(shù),復(fù)制一個(gè)復(fù)雜鏈表。在復(fù)雜鏈表中,每個(gè)節(jié)點(diǎn)除了有一個(gè) next 指針指向下一個(gè)節(jié)點(diǎn),還有一個(gè) random 指針指向鏈表中的任意節(jié)點(diǎn)或者 null。
示例 1:
輸入:head = [[7,null],[13,0],[11,4],[10,2],[1,0]] 輸出:[[7,null],[13,0],[11,4],[10,2],[1,0]]
示例 2:
輸入:head = [[1,1],[2,1]] 輸出:[[1,1],[2,1]]
示例 3:
輸入:head = [[3,null],[3,0],[3,null]] 輸出:[[3,null],[3,0],[3,null]]
示例 4:
輸入:head = [] 輸出:[] 解釋?zhuān)航o定的鏈表為空(空指針),因此返回 null。
提示: -10000 <= Node.val <= 10000 Node.random 為空(null)或指向鏈表中的節(jié)點(diǎn)。 節(jié)點(diǎn)數(shù)目不超過(guò) 1000 。
解題思路
哈希的做法,在大多數(shù)公司的面試官面前并不是一個(gè)滿(mǎn)意的答案,所以需要知道原地修改的解法才能夠從容面對(duì)面試。 原地修改解法流程: 假設(shè)三個(gè)節(jié)點(diǎn)初始如下
1.第一次遍歷,復(fù)制一個(gè)新的節(jié)點(diǎn)在原有節(jié)點(diǎn)之后,如 1 -> 2 -> 3 -> null 復(fù)制完就是 1 -> 1
-> 2 -> 2
-> 3 - > 3
-> null 第一次遍歷,構(gòu)建的節(jié)點(diǎn),random還未連接起來(lái),如下圖
我們需要把A
指向C
,因?yàn)槌跏嫉腁的random指針指向了C,那是不是有這樣的公式: A
->random = A->random->next
2.第二次遍歷,從頭開(kāi)始遍歷鏈表,通過(guò) cur.next.random = cur.random.next 可以將復(fù)制節(jié)點(diǎn)的隨機(jī)指針串起來(lái),當(dāng)然需要判斷 cur.random 是否存在
3.第三次遍歷,就比較簡(jiǎn)單了,只是找出這些相鄰節(jié)點(diǎn),組成結(jié)果就可以
class Solution { public Node copyRandomList(Node head) { // if head == null,則return null if (head == null) { return null; } // 第一次遍歷, 1 -> `1` -> 2 -> `2` -> 3 - > `3` -> null Node cur = head; while (cur != null) { Node node = new Node(cur.val); Node temp = cur.next; cur.next = node; cur.next.next = temp; cur = cur.next.next; } // 第二次遍歷,填充random節(jié)點(diǎn) cur = head; while (cur != null) { Node newNode = cur.next; newNode.random = cur.random != null ? cur.random.next : null; cur = cur.next.next; } // 第三次遍歷,拆分 Node headNew = head.next; for (Node node = head; node != null; node = node.next) { Node nodeNew = node.next; node.next = node.next.next; nodeNew.next = (nodeNew.next != null) ? nodeNew.next.next : null; } return headNew; } class Node { int val; Node next; Node random; public Node(int val) { this.val = val; this.next = null; this.random = null; } } }
以上就是java算法題解LeetCode35復(fù)雜鏈表的復(fù)制實(shí)例的詳細(xì)內(nèi)容,更多關(guān)于java算法復(fù)雜鏈表復(fù)制的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
解決Maven項(xiàng)目pom.xml導(dǎo)入了Junit包還是用不了@Test注解問(wèn)題
在Maven項(xiàng)目中,如果在非test目錄下使用@Test注解,可能會(huì)因?yàn)閜om.xml中<scope>test</scope>的設(shè)置而無(wú)法使用,正確做法是將測(cè)試代碼放在src/test/java目錄下,或去除<scope>test</scope>限制,這樣可以確保Junit依賴(lài)正確加載并應(yīng)用于適當(dāng)?shù)拇a部分2024-10-10Java實(shí)現(xiàn)Fibonacci(斐波那契)取余的示例代碼
這篇文章主要介紹了Java實(shí)現(xiàn)Fibonacci取余的示例代碼,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-03-03SpringBoot 如何根據(jù)不同profile選擇不同配置
這篇文章主要介紹了SpringBoot 如何根據(jù)不同profile選擇不同配置的操作方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-08-08ArrayList詳解和使用示例_動(dòng)力節(jié)點(diǎn)Java學(xué)院整理
ArrayList 是一個(gè)數(shù)組隊(duì)列,相當(dāng)于 動(dòng)態(tài)數(shù)組。與Java中的數(shù)組相比,它的容量能動(dòng)態(tài)增長(zhǎng)。接下來(lái)通過(guò)本文給大家介紹arraylist詳解和使用示例代碼,需要的的朋友一起學(xué)習(xí)吧2017-05-05java使用Nagao算法實(shí)現(xiàn)新詞發(fā)現(xiàn)、熱門(mén)詞的挖掘
這篇文章主要介紹了java使用Nagao算法實(shí)現(xiàn)新詞發(fā)現(xiàn)、熱門(mén)詞的挖掘的思路和詳細(xì)代碼,需要的朋友可以參考下2015-07-07