日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程语言 > java >内容正文

java

[Java]LeetCode138. 复制带随机指针的链表 | Copy List with Random Pointer

發(fā)布時(shí)間:2025/3/15 java 33 豆豆
生活随笔 收集整理的這篇文章主要介紹了 [Java]LeetCode138. 复制带随机指针的链表 | Copy List with Random Pointer 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
?微信公眾號(hào):山青詠芝(shanqingyongzhi)
?博客園地址:山青詠芝(https://www.cnblogs.com/strengthen/)
?GitHub地址:https://github.com/strengthen/LeetCode
?原文地址:https://www.cnblogs.com/strengthen/p/9967399.html?
?如果鏈接不是山青詠芝的博客園地址,則可能是爬取作者的文章。
?原文已修改更新!強(qiáng)烈建議點(diǎn)擊原文地址閱讀!支持作者!支持原創(chuàng)!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★

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.


給定一個(gè)鏈表,每個(gè)節(jié)點(diǎn)包含一個(gè)額外增加的隨機(jī)指針,該指針可以指向鏈表中的任何節(jié)點(diǎn)或空節(jié)點(diǎn)。

要求返回這個(gè)鏈表的深度拷貝。?


1ms

1 /** 2 * Definition for singly-linked list with a random pointer. 3 * class RandomListNode { 4 * int label; 5 * RandomListNode next, random; 6 * RandomListNode(int x) { this.label = x; } 7 * }; 8 */ 9 public class Solution { 10 public RandomListNode copyRandomList(RandomListNode head) { 11 RandomListNode res = new RandomListNode(0); 12 RandomListNode p = res; 13 RandomListNode curr = head; 14 while (curr != null) { 15 p.next = clone(curr); 16 curr = curr.next; 17 p = p.next; 18 } 19 return res.next; 20 } 21 22 private RandomListNode clone(RandomListNode node) { 23 RandomListNode newNode = new RandomListNode(node.label); 24 if (node.random != null) 25 newNode.random = new RandomListNode(node.random.label); 26 return newNode; 27 } 28 }

2ms

1 /** 2 * Definition for singly-linked list with a random pointer. 3 * class RandomListNode { 4 * int label; 5 * RandomListNode next, random; 6 * RandomListNode(int x) { this.label = x; } 7 * }; 8 */ 9 public class Solution { 10 public RandomListNode copyRandomList(RandomListNode head) { 11 if(head == null) return head; 12 copyNext(head); 13 copyRandom(head); 14 15 RandomListNode curr = head; 16 RandomListNode newHead = head.next; 17 RandomListNode start = newHead; 18 while(curr!=null && curr.next!=null){ 19 RandomListNode next = curr.next.next; 20 curr.next = next; 21 curr = next; 22 23 if(start.next!=null) { 24 start.next = start.next.next; 25 start = start.next; 26 } 27 } 28 29 return newHead; 30 } 31 32 private void copyNext(RandomListNode head){ 33 if(head == null) return; 34 35 RandomListNode curr = head; 36 while(curr!=null){ 37 RandomListNode next = curr.next; 38 curr.next = new RandomListNode(curr.label); 39 curr.next.random = curr.random; 40 curr.next.next = next; 41 curr = next; 42 } 43 } 44 45 private void copyRandom(RandomListNode head) { 46 if(head == null) return; 47 RandomListNode curr = head; 48 while(curr!=null && curr.next!=null){ 49 RandomListNode next = curr.next.next; 50 if(curr.next!=null && curr.random!=null){ 51 curr.next.random = curr.random.next; 52 } 53 curr = next; 54 } 55 } 56 }

2ms

1 /** 2 * Definition for singly-linked list with a random pointer. 3 * class RandomListNode { 4 * int label; 5 * RandomListNode next, random; 6 * RandomListNode(int x) { this.label = x; } 7 * }; 8 */ 9 public class Solution { 10 public RandomListNode copyRandomList(RandomListNode head) 11 { 12 RandomListNode root = null, prev = null, rootIter = head; 13 14 while (rootIter != null) 15 { 16 RandomListNode newCopy = new RandomListNode(rootIter.label); 17 newCopy.next = null; 18 newCopy.random = null; 19 20 prev = rootIter; 21 rootIter = rootIter.next; 22 23 prev.next = newCopy; 24 newCopy.next = rootIter; 25 /* 'prev' points to previous rootIter whose 'next' will point 26 to the new copy of previous rootIter created in 'newCopy' */ 27 } 28 if (head == null) 29 root = null; 30 31 else 32 root = head.next; 33 34 rootIter = head; 35 while (rootIter != null) 36 { 37 if (rootIter.random == null) 38 rootIter.next.random = null; 39 40 else 41 rootIter.next.random = rootIter.random.next; 42 43 rootIter = rootIter.next.next; 44 } 45 46 rootIter = head; 47 while (rootIter != null) 48 { 49 RandomListNode nextNode = rootIter.next.next; 50 if (nextNode != null) 51 rootIter.next.next = nextNode.next; 52 53 rootIter.next = nextNode; 54 rootIter = nextNode; 55 } 56 return root; 57 } 58 }

3ms

1 /** 2 * Definition for singly-linked list with a random pointer. 3 * class RandomListNode { 4 * int label; 5 * RandomListNode next, random; 6 * RandomListNode(int x) { this.label = x; } 7 * }; 8 */ 9 public class Solution { 10 11 private Map<RandomListNode,RandomListNode> visited = new HashMap<>(); 12 13 public RandomListNode copyRandomList(RandomListNode head) { 14 if(head == null){ 15 return null; 16 } 17 18 if(visited.containsKey(head)){ 19 return visited.get(head); 20 } 21 22 RandomListNode newNode = new RandomListNode(head.label); 23 visited.put(head,newNode); //at this point in time, we have newed this node. does not matter if we need to recurse further to have it fully constructed. this is already copied. thus mark it as visited 24 25 newNode.next = copyRandomList(head.next); 26 newNode.random = copyRandomList(head.random); 27 28 29 return newNode; 30 31 } 32 }

?

轉(zhuǎn)載于:https://www.cnblogs.com/strengthen/p/9967399.html

總結(jié)

以上是生活随笔為你收集整理的[Java]LeetCode138. 复制带随机指针的链表 | Copy List with Random Pointer的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。