日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 >

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

發布時間:2025/3/15 37 豆豆
生活随笔 收集整理的這篇文章主要介紹了 [Java]LeetCode138. 复制带随机指针的链表 | Copy List with Random Pointer 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

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

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.


給定一個鏈表,每個節點包含一個額外增加的隨機指針,該指針可以指向鏈表中的任何節點或空節點。

要求返回這個鏈表的深度拷貝。?


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 }

?

轉載于:https://www.cnblogs.com/strengthen/p/9967399.html

總結

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

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。