Skip to content

Commit d916a95

Browse files
authored
Improved task 138
1 parent f8de2c0 commit d916a95

File tree

3 files changed

+35
-69
lines changed

3 files changed

+35
-69
lines changed

src/main/java/com_github_leetcode/random/Node.java

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,20 +8,10 @@ public class Node {
88
public Node next;
99
public Node random;
1010

11-
public Node() {
12-
this.val = 0;
13-
}
14-
1511
public Node(int val) {
1612
this.val = val;
1713
}
1814

19-
public Node(int val, Node next, Node random) {
20-
this.val = val;
21-
this.next = next;
22-
this.random = random;
23-
}
24-
2515
public String toString() {
2616
StringJoiner result = new StringJoiner(",", "[", "]");
2717
StringJoiner result2 = new StringJoiner(",", "[", "]");

src/main/java/g0101_0200/s0138_copy_list_with_random_pointer/Solution.java

Lines changed: 23 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -2,70 +2,41 @@
22

33
// #Medium #Top_100_Liked_Questions #Top_Interview_Questions #Hash_Table #Linked_List
44
// #Programming_Skills_II_Day_14 #Udemy_Linked_List #Top_Interview_150_Linked_List
5-
// #Big_O_Time_O(N)_Space_O(N) #2024_11_13_Time_0_ms_(100.00%)_Space_44.1_MB_(92.12%)
5+
// #Big_O_Time_O(N)_Space_O(N) #2025_07_04_Time_0_ms_(100.00%)_Space_43.96_MB_(99.29%)
66

77
import com_github_leetcode.random.Node;
8+
import java.util.HashMap;
9+
import java.util.Map;
810

911
/*
1012
// Definition for a Node.
1113
class Node {
12-
public int val;
13-
public Node next;
14-
public Node random;
14+
int val;
15+
Node next;
16+
Node random;
1517
16-
public Node() {}
17-
18-
public Node(int _val,Node _next,Node _random) {
19-
val = _val;
20-
next = _next;
21-
random = _random;
18+
public Node(int val) {
19+
this.val = val;
20+
this.next = null;
21+
this.random = null;
2222
}
23-
};
23+
}
2424
*/
2525
public class Solution {
2626
public Node copyRandomList(Node head) {
27-
if (head == null) {
28-
return null;
29-
}
30-
// first pass to have a clone node point to the next node. ie A->B becomes A->clonedNode->B
31-
Node curr = head;
32-
while (curr != null) {
33-
Node clonedNode = new Node(curr.val);
34-
clonedNode.next = curr.next;
35-
curr.next = clonedNode;
36-
curr = clonedNode.next;
37-
}
38-
curr = head;
39-
// second pass to make the cloned node's random pointer point to the orginal node's randome
40-
// pointer.
41-
// ie. A's random pointer becomes ClonedNode's random pointer
42-
while (curr != null) {
43-
if (curr.random != null) {
44-
curr.next.random = curr.random.next;
45-
} else {
46-
curr.next.random = null;
47-
}
48-
curr = curr.next.next;
27+
Map<Node, Node> hashMap = new HashMap<>();
28+
Node cur = head;
29+
while (cur != null) {
30+
hashMap.put(cur, new Node(cur.val));
31+
cur = cur.next;
4932
}
50-
curr = head;
51-
// third pass to restore the links and return the head of the cloned nodes' list.
52-
Node newHead = null;
53-
while (curr != null) {
54-
Node clonedNode;
55-
if (newHead == null) {
56-
clonedNode = curr.next;
57-
newHead = clonedNode;
58-
} else {
59-
clonedNode = curr.next;
60-
}
61-
curr.next = clonedNode.next;
62-
if (curr.next != null) {
63-
clonedNode.next = curr.next.next;
64-
} else {
65-
clonedNode.next = null;
66-
}
67-
curr = curr.next;
33+
cur = head;
34+
while (cur != null) {
35+
Node copy = hashMap.get(cur);
36+
copy.next = hashMap.get(cur.next);
37+
copy.random = hashMap.get(cur.random);
38+
cur = cur.next;
6839
}
69-
return newHead;
40+
return hashMap.get(head);
7041
}
7142
}

src/test/java/com_github_leetcode/random/NodeTest.java

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
class NodeTest {
99
@Test
1010
void constructor() {
11-
Node node = new Node();
11+
Node node = new Node(0);
1212
assertThat(node.val, equalTo(0));
1313
assertThat(node.toString(), equalTo("[[0,null]]"));
1414
}
@@ -22,18 +22,23 @@ void constructor2() {
2222

2323
@Test
2424
void constructor3() {
25-
Node node = new Node(1, new Node(2), new Node(3));
25+
Node node = new Node(1);
26+
node.next = new Node(2);
27+
node.random = new Node(3);
2628
assertThat(node.val, equalTo(1));
2729
assertThat(node.toString(), equalTo("[[1,3],[2,null]]"));
2830
}
2931

3032
@Test
3133
void constructor4() {
32-
Node node =
33-
new Node(
34-
1,
35-
new Node(2, new Node(21), new Node(22)),
36-
new Node(3, null, new Node(32)));
34+
Node next = new Node(2);
35+
next.next = new Node(21);
36+
next.random = new Node(22);
37+
Node random = new Node(3);
38+
random.random = new Node(32);
39+
Node node = new Node(1);
40+
node.next = next;
41+
node.random = random;
3742
assertThat(node.val, equalTo(1));
3843
assertThat(node.toString(), equalTo("[[1,3],[2,2],[21,null]]"));
3944
}

0 commit comments

Comments
 (0)