|
2 | 2 |
|
3 | 3 | // #Medium #Top_100_Liked_Questions #Top_Interview_Questions #Hash_Table #Linked_List
|
4 | 4 | // #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%) |
6 | 6 |
|
7 | 7 | import com_github_leetcode.random.Node;
|
| 8 | +import java.util.HashMap; |
| 9 | +import java.util.Map; |
8 | 10 |
|
9 | 11 | /*
|
10 | 12 | // Definition for a Node.
|
11 | 13 | class Node {
|
12 |
| - public int val; |
13 |
| - public Node next; |
14 |
| - public Node random; |
| 14 | + int val; |
| 15 | + Node next; |
| 16 | + Node random; |
15 | 17 |
|
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; |
22 | 22 | }
|
23 |
| -}; |
| 23 | +} |
24 | 24 | */
|
25 | 25 | public class Solution {
|
26 | 26 | 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; |
49 | 32 | }
|
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; |
68 | 39 | }
|
69 |
| - return newHead; |
| 40 | + return hashMap.get(head); |
70 | 41 | }
|
71 | 42 | }
|
0 commit comments