Advertisement
unknown_0711

Untitled

Nov 15th, 2022
23
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.51 KB | None | 0 0
  1. public RandomListNode copyRandomList(RandomListNode head) {
  2. if (head == null) return null;
  3.  
  4. Map<RandomListNode, RandomListNode> map = new HashMap<RandomListNode, RandomListNode>();
  5. RandomListNode node = head;
  6. while (node != null) {
  7. map.put(node, new RandomListNode(node.label));
  8. node = node.next;
  9. }
  10. node = head;
  11. while (node != null) {
  12. map.get(node).next = map.get(node.next);
  13. map.get(node).random = map.get(node.random);
  14. node = node.next;
  15. }
  16. return map.get(head);
  17. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement