Copy List with Random Pointer@LeetCode
文章目录
Copy List with Random Pointer
节点中需要拷贝的两个引用是next和random。next引用比较好拷贝,相当直接复制普通列表。而对于random则需要目标节点已存在才比较容易些拷贝代码,采用的办法就是构造一个HashMap,其中key是原节点,value是拷贝节点,在拷贝random引用的过程中,直接用map.get(node.random)来获取相应的目标节点即可。
实现代码:
java/** * Definition for singly-linked list with a random pointer. * class RandomListNode { * int label; * RandomListNode next, random; * RandomListNode(int x) { this.label = x; } * }; */ public class Solution { public RandomListNode copyRandomList(RandomListNode head) { if (head == null) return null; RandomListNode targetNode = head, copyPreHead = new RandomListNode(-1), copyNode = copyPreHead; HashMap<RandomListNode, RandomListNode> copiedMap = new HashMap<RandomListNode, RandomListNode>(); while (targetNode != null) { copyNode.next = new RandomListNode(targetNode.label); copiedMap.put(targetNode, copyNode.next); targetNode = targetNode.next; copyNode = copyNode.next; } targetNode = head; copyNode = copyPreHead.next; while (targetNode != null) { if (targetNode.random != null) { copyNode.random = copiedMap.get(targetNode.random); } targetNode = targetNode.next; copyNode = copyNode.next; } return copyPreHead.next; } }
评论
登录后参与技术讨论,分享你的经验与见解。
登录 / 注册暂无评论