-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCopyRandomList.java
More file actions
31 lines (31 loc) · 1.11 KB
/
CopyRandomList.java
File metadata and controls
31 lines (31 loc) · 1.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
import java.util.HashMap;
import java.util.Random;
public class CopyRandomList {
// public Node copyRandomList(Node head) {
// if(head == null) return head;
// //for having a track of random mapping
// HashMap<Node,Node> map = new HashMap<>();
// //creating the duplicate with the start same as head.val
// Node start = new Node(head.val);
//
// //making the node1 with head.next val and node2 as with head
// Node node1 = head.next;
// Node node2 = start;
// //having the map of original and creating dup
// map.put(head,start);
// while(node1 != null){
// node2.next = new Node(node1.val);//adding link with next val as node1.val
// node2 = node2.next;// moving the node to point to next
// map.put(node1,node2);// updating the map
// node1 = node1.next;
// }
// node1 = head;
// node2 = start;
// while(node2 != null){
// node2.random = map.get(node1.random);
// node2 = node2.next;
// node1 = node1.next;
// }
// return start;
// }
}