[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 }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了