-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLC138_CopyListWithRandomPointer.java
More file actions
105 lines (89 loc) · 2.84 KB
/
LC138_CopyListWithRandomPointer.java
File metadata and controls
105 lines (89 loc) · 2.84 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
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
103
104
105
package practise.src.main.java.leetCode;
import java.util.HashMap;
import java.util.Map;
public class LC138_CopyListWithRandomPointer {
static class Node {
int val;
Node next;
Node random;
public Node(int val) {
this.val = val;
this.next = null;
this.random = null;
}
}
// Helper method to build the list from input
public static Node buildList(int[][] input) {
Node[] nodes = new Node[input.length];
// Create nodes
for (int i = 0; i < input.length; i++) {
nodes[i] = new Node(input[i][0]);
}
// Set next pointers
for (int i = 0; i < nodes.length - 1; i++) {
nodes[i].next = nodes[i + 1];
}
// Set random pointers
for (int i = 0; i < input.length; i++) {
int randomIndex = input[i][1];
if (randomIndex != -1) {
nodes[i].random = nodes[randomIndex];
}
}
return nodes[0];
}
public static Node copyRandomList(Node head) {
Node cur = head;
HashMap<Node, Node> map = new HashMap<>();
while(cur != null) {
map.put(cur, new Node(cur.val));
cur = cur.next;
}
cur = head;
while(cur != null) {
Node copy = map.get(cur);
copy.next = map.get(cur.next);
copy.random = map.get(cur.random);
cur = cur.next;
}
return map.get(head);
}
public static void main(String[] args) {
// Input: [[7,null],[13,0],[11,4],[10,2],[1,0]]
// Using -1 to represent null
int[][] input = {
{7, -1},
{13, 0},
{11, 4},
{10, 2},
{1, 0}
};
Node head = buildList(input);
System.out.println("Original array:: ");
printList(head);
Node copiedHead = copyRandomList(head);
System.out.println("Copied array:: ");
printList(copiedHead);
}
public static void printList(Node head) {
Map<Node, Integer> indexMap = new HashMap<>();
Node curr = head;
int index = 0;
// First pass: store index of each node
while (curr != null) {
indexMap.put(curr, index++);
curr = curr.next;
}
// Second pass: print nodes
curr = head;
while (curr != null) {
int val = curr.val;
String randomIndex = (curr.random == null)
? "null"
: String.valueOf(indexMap.get(curr.random));
System.out.print("[" + val + "," + randomIndex + "] ");
curr = curr.next;
}
System.out.println();
}
}