|
1 |
| -import java.util.Set; |
2 |
| -import java.util.Stack; |
3 |
| - |
4 | 1 | 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; |
18 | 11 | }
|
19 |
| - return stack.peek(); |
20 | 12 | }
|
21 | 13 |
|
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); |
24 | 39 | }
|
25 | 40 |
|
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; |
34 | 66 | }
|
35 | 67 | }
|
0 commit comments