Skip to content

Commit 9d46e88

Browse files
update Copy List with Random Pointer
1 parent 1fe65c6 commit 9d46e88

File tree

2 files changed

+88
-46
lines changed

2 files changed

+88
-46
lines changed

src/CopyListWithRandomPointer.java

Lines changed: 29 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// https://leetcode.com/problems/copy-list-with-random-pointer
22
// T: O(N)
3-
// S: O(N)
3+
// S: O(1)
44

55
import java.util.HashMap;
66
import java.util.Map;
@@ -18,30 +18,40 @@ public Node(int val) {
1818
}
1919
}
2020

21-
private static final Map<Node, Node> nodes = new HashMap<>();
21+
public HelloWorld.Node copyRandomList(HelloWorld.Node head) {
22+
if (head == null) {
23+
return null;
24+
}
2225

23-
public Node copyRandomList(Node head) {
24-
if (head == null) return null;
26+
createWeavedList(head);
27+
linkRandomPointersForNewNodes(head);
28+
return unweaveList(head);
29+
}
2530

26-
for (Node current = head ; current != null ; current = current.next) {
27-
Node node = getNode(current);
28-
if (current.next != null) {
29-
node.next = getNode(current.next);
30-
}
31-
if (current.random != null) {
32-
node.random = getNode(current.random);
33-
}
31+
// A->B->C --> A->A'->B->B'
32+
private static void createWeavedList(HelloWorld.Node head) {
33+
for (HelloWorld.Node i = head; i != null ; i = i.next.next) {
34+
HelloWorld.Node newNode = new HelloWorld.Node(i.val);
35+
newNode.next = i.next;
36+
i.next = newNode;
3437
}
38+
}
3539

36-
return getNode(head);
40+
private static void linkRandomPointersForNewNodes(HelloWorld.Node head) {
41+
for (HelloWorld.Node i = head; i != null ; i = i.next.next) {
42+
if (i.random == null) {
43+
continue;
44+
}
45+
i.next.random = i.random.next;
46+
}
3747
}
3848

39-
private Node getNode(Node node) {
40-
if (nodes.containsKey(node)) {
41-
return nodes.get(node);
49+
private static HelloWorld.Node unweaveList(HelloWorld.Node head) {
50+
final HelloWorld.Node pointerNew = head.next;
51+
for (HelloWorld.Node old = head, i = head.next; i != null && old != null ; i = i.next, old = old.next) {
52+
old.next = old.next == null ? null : old.next.next;
53+
i.next = i.next == null ? null : i.next.next;
4254
}
43-
Node copyNode = new Node(node.val);
44-
nodes.put(node, copyNode);
45-
return copyNode;
55+
return pointerNew;
4656
}
4757
}

src/HelloWorld.java

Lines changed: 59 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,67 @@
1-
import java.util.Set;
2-
import java.util.Stack;
3-
41
public class HelloWorld {
5-
private static final Set<String> OPERATORS = Set.of("+", "-", "*", "/");
6-
7-
public int evalRPN(String[] tokens) {
8-
final Stack<Integer> stack = new Stack<>();
9-
for (String token : tokens) {
10-
if (isOperator(token)) {
11-
final int second = stack.pop();
12-
final int first = stack.pop();
13-
final int result = apply(token, first, second);
14-
stack.push(result);
15-
} else {
16-
stack.push(Integer.parseInt(token));
17-
}
2+
public static class Node {
3+
int val;
4+
Node next;
5+
Node random;
6+
7+
public Node(int val) {
8+
this.val = val;
9+
this.next = null;
10+
this.random = null;
1811
}
19-
return stack.peek();
2012
}
2113

22-
private static boolean isOperator(String token) {
23-
return OPERATORS.contains(token);
14+
/*
15+
// Definition for a Node.
16+
class Node {
17+
public int val;
18+
public Node next;
19+
public Node random;
20+
21+
public Node() {}
22+
23+
public Node(int _val,Node _next,Node _random) {
24+
val = _val;
25+
next = _next;
26+
random = _random;
27+
}
28+
};
29+
*/
30+
31+
public Node copyRandomList(Node head) {
32+
if (head == null) {
33+
return null;
34+
}
35+
36+
createWeavedList(head);
37+
linkRandomPointersForNewNodes(head);
38+
return unweaveList(head);
2439
}
2540

26-
private static int apply(String operator, int first, int second) {
27-
return switch (operator) {
28-
case "+" -> first + second;
29-
case "-" -> first - second;
30-
case "/" -> first / second;
31-
case "*" -> first * second;
32-
default -> 0;
33-
};
41+
// A->B->C --> A->A'->B->B'
42+
private static void createWeavedList(Node head) {
43+
for (Node i = head ; i != null ; i = i.next.next) {
44+
Node newNode = new Node(i.val);
45+
newNode.next = i.next;
46+
i.next = newNode;
47+
}
48+
}
49+
50+
private static void linkRandomPointersForNewNodes(Node head) {
51+
for (Node i = head ; i != null ; i = i.next.next) {
52+
if (i.random == null) {
53+
continue;
54+
}
55+
i.next.random = i.random.next;
56+
}
57+
}
58+
59+
private static Node unweaveList(Node head) {
60+
final Node pointerNew = head.next;
61+
for (Node old = head, i = head.next ; i != null && old != null ; i = i.next, old = old.next) {
62+
old.next = old.next == null ? null : old.next.next;
63+
i.next = i.next == null ? null : i.next.next;
64+
}
65+
return pointerNew;
3466
}
3567
}

0 commit comments

Comments
 (0)