-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCopy List with Random Pointer.java
102 lines (77 loc) · 2.79 KB
/
Copy List with Random Pointer.java
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
// https://leetcode.com/problems/copy-list-with-random-pointer/
/*
// Definition for a Node.
class Node {
int val;
Node next;
Node random;
public Node(int val) {
this.val = val;
this.next = null;
this.random = null;
}
}
*/
/*
Map of memory addresses{
original node: new node
}
Steps:
1. Create a new list based on next pointer
2. For every new node, store original node and clone to map where original node is key and clone is value.
- In this step, map is storing the memory addresses of each node.
3. Iterate original list again and find node being pointed by random pointer in map
4. Connect new node to the random node's clone
===
Time Complexity: O(n) where n = size of linked list
Space Complexity: O(n) where n = size of linked list. The size affects the size of the map and cloned linked list.
*/
class Solution {
private Node createCloneLinkedList(Node head, Map<Node, Node> nodeMap) {
// Create new linked list
Node origNode = head;
Node cloneNode = new Node(0);
Node dummyNode = new Node(0);
dummyNode.next = cloneNode;
// Iterate original linked list
while (origNode != null) {
// Override default value
cloneNode.val = origNode.val;
// Insert memory address of each node to map
nodeMap.put(origNode, cloneNode);
origNode = origNode.next;
// IMPORTANT: Added this because if I didn't, cloned linked list would end with an extra node
if (origNode != null) {
cloneNode.next = new Node(0);
cloneNode = cloneNode.next;
}
}
return dummyNode.next;
}
private void populateRandomPointers(Node origHead, Node cloneHead, Map<Node, Node> nodeMap) {
Node origNode = origHead;
Node cloneNode = cloneHead;
// IMPORTANT: After a successful cloning, both linked lists are the same size
while (origNode != null) {
// If random pointer is null, move to next node in both linked lists
if (origNode.random == null) {
origNode = origNode.next;
cloneNode = cloneNode.next;
continue;
}
// Get clone of random pointer and update curNode in cloned linked list
cloneNode.random = nodeMap.get(origNode.random);
origNode = origNode.next;
cloneNode = cloneNode.next;
}
}
public Node copyRandomList(Node head) {
if (head == null) {
return null;
}
Map<Node, Node> nodeMap = new HashMap<>();
Node cloneHead = createCloneLinkedList(head, nodeMap);
populateRandomPointers(head, cloneHead, nodeMap);
return cloneHead;
}
}