From cf4d9362958c6082ba37e3aa114c12c52e74fea0 Mon Sep 17 00:00:00 2001 From: Valentyn Kolesnikov Date: Fri, 4 Jul 2025 06:36:53 +0300 Subject: [PATCH] Improved task 138 --- .../java/com_github_leetcode/random/Node.java | 10 --- .../Solution.java | 75 ++++++------------- .../com_github_leetcode/random/NodeTest.java | 19 +++-- 3 files changed, 35 insertions(+), 69 deletions(-) diff --git a/src/main/java/com_github_leetcode/random/Node.java b/src/main/java/com_github_leetcode/random/Node.java index 7d1e9438c..585b46c54 100644 --- a/src/main/java/com_github_leetcode/random/Node.java +++ b/src/main/java/com_github_leetcode/random/Node.java @@ -8,20 +8,10 @@ public class Node { public Node next; public Node random; - public Node() { - this.val = 0; - } - public Node(int val) { this.val = val; } - public Node(int val, Node next, Node random) { - this.val = val; - this.next = next; - this.random = random; - } - public String toString() { StringJoiner result = new StringJoiner(",", "[", "]"); StringJoiner result2 = new StringJoiner(",", "[", "]"); diff --git a/src/main/java/g0101_0200/s0138_copy_list_with_random_pointer/Solution.java b/src/main/java/g0101_0200/s0138_copy_list_with_random_pointer/Solution.java index 296a29a84..32cb71404 100644 --- a/src/main/java/g0101_0200/s0138_copy_list_with_random_pointer/Solution.java +++ b/src/main/java/g0101_0200/s0138_copy_list_with_random_pointer/Solution.java @@ -2,70 +2,41 @@ // #Medium #Top_100_Liked_Questions #Top_Interview_Questions #Hash_Table #Linked_List // #Programming_Skills_II_Day_14 #Udemy_Linked_List #Top_Interview_150_Linked_List -// #Big_O_Time_O(N)_Space_O(N) #2024_11_13_Time_0_ms_(100.00%)_Space_44.1_MB_(92.12%) +// #Big_O_Time_O(N)_Space_O(N) #2025_07_04_Time_0_ms_(100.00%)_Space_43.96_MB_(99.29%) import com_github_leetcode.random.Node; +import java.util.HashMap; +import java.util.Map; /* // Definition for a Node. class Node { - public int val; - public Node next; - public Node random; + int val; + Node next; + Node random; - public Node() {} - - public Node(int _val,Node _next,Node _random) { - val = _val; - next = _next; - random = _random; + public Node(int val) { + this.val = val; + this.next = null; + this.random = null; } -}; +} */ public class Solution { public Node copyRandomList(Node head) { - if (head == null) { - return null; - } - // first pass to have a clone node point to the next node. ie A->B becomes A->clonedNode->B - Node curr = head; - while (curr != null) { - Node clonedNode = new Node(curr.val); - clonedNode.next = curr.next; - curr.next = clonedNode; - curr = clonedNode.next; - } - curr = head; - // second pass to make the cloned node's random pointer point to the orginal node's randome - // pointer. - // ie. A's random pointer becomes ClonedNode's random pointer - while (curr != null) { - if (curr.random != null) { - curr.next.random = curr.random.next; - } else { - curr.next.random = null; - } - curr = curr.next.next; + Map hashMap = new HashMap<>(); + Node cur = head; + while (cur != null) { + hashMap.put(cur, new Node(cur.val)); + cur = cur.next; } - curr = head; - // third pass to restore the links and return the head of the cloned nodes' list. - Node newHead = null; - while (curr != null) { - Node clonedNode; - if (newHead == null) { - clonedNode = curr.next; - newHead = clonedNode; - } else { - clonedNode = curr.next; - } - curr.next = clonedNode.next; - if (curr.next != null) { - clonedNode.next = curr.next.next; - } else { - clonedNode.next = null; - } - curr = curr.next; + cur = head; + while (cur != null) { + Node copy = hashMap.get(cur); + copy.next = hashMap.get(cur.next); + copy.random = hashMap.get(cur.random); + cur = cur.next; } - return newHead; + return hashMap.get(head); } } diff --git a/src/test/java/com_github_leetcode/random/NodeTest.java b/src/test/java/com_github_leetcode/random/NodeTest.java index 248e95875..ab91f51ca 100644 --- a/src/test/java/com_github_leetcode/random/NodeTest.java +++ b/src/test/java/com_github_leetcode/random/NodeTest.java @@ -8,7 +8,7 @@ class NodeTest { @Test void constructor() { - Node node = new Node(); + Node node = new Node(0); assertThat(node.val, equalTo(0)); assertThat(node.toString(), equalTo("[[0,null]]")); } @@ -22,18 +22,23 @@ void constructor2() { @Test void constructor3() { - Node node = new Node(1, new Node(2), new Node(3)); + Node node = new Node(1); + node.next = new Node(2); + node.random = new Node(3); assertThat(node.val, equalTo(1)); assertThat(node.toString(), equalTo("[[1,3],[2,null]]")); } @Test void constructor4() { - Node node = - new Node( - 1, - new Node(2, new Node(21), new Node(22)), - new Node(3, null, new Node(32))); + Node next = new Node(2); + next.next = new Node(21); + next.random = new Node(22); + Node random = new Node(3); + random.random = new Node(32); + Node node = new Node(1); + node.next = next; + node.random = random; assertThat(node.val, equalTo(1)); assertThat(node.toString(), equalTo("[[1,3],[2,2],[21,null]]")); }