|
1 |
| -package hashtable; |
2 |
| - |
3 |
| -import java.util.HashMap; |
4 |
| -import java.util.Map; |
5 |
| - |
6 |
| -import org.junit.Test; |
7 |
| - |
8 |
| -// Definition for singly-linked list with a random pointer. |
9 |
| - class RandomListNode |
10 |
| - { |
11 |
| - int label; |
12 |
| - RandomListNode next, random; |
13 |
| - RandomListNode(int x) { this.label = x; } |
14 |
| - }; |
15 |
| - |
16 |
| -public class CopyListWithRandomPointer |
17 |
| -{ |
18 |
| - public RandomListNode copyRandomListHashMap( RandomListNode head ) |
19 |
| - { |
20 |
| - if ( head == null ) |
21 |
| - { |
22 |
| - return null; |
23 |
| - } |
24 |
| - |
25 |
| - Map<RandomListNode, RandomListNode> map = new HashMap<RandomListNode, RandomListNode>(); |
26 |
| - RandomListNode dummy = new RandomListNode(0); |
27 |
| - RandomListNode pre = dummy, newNode; |
28 |
| - while ( head != null ) |
29 |
| - { |
30 |
| - if ( map.containsKey( head ) ) |
31 |
| - { |
32 |
| - newNode = map.get( head ); |
33 |
| - } |
34 |
| - else |
35 |
| - { |
36 |
| - newNode = new RandomListNode( head.label ); |
37 |
| - map.put( head, newNode ); |
38 |
| - } |
39 |
| - pre.next = newNode; |
40 |
| - |
41 |
| - if ( head.random != null ) |
42 |
| - { |
43 |
| - if ( map.containsKey( head.random ) ) |
44 |
| - { |
45 |
| - newNode.random = map.get( head.random ); |
46 |
| - } |
47 |
| - else |
48 |
| - { |
49 |
| - newNode.random = new RandomListNode( head.random.label ); |
50 |
| - map.put( head.random, newNode.random ); |
51 |
| - } |
52 |
| - } |
53 |
| - |
54 |
| - pre = newNode; |
55 |
| - head = head.next; |
56 |
| - } |
57 |
| - |
58 |
| - return dummy.next; |
59 |
| - } |
60 |
| - |
61 |
| - public RandomListNode copyRandomList( RandomListNode head ) |
62 |
| - { |
63 |
| - if ( head == null ) |
64 |
| - { |
65 |
| - return null; |
66 |
| - } |
67 |
| - |
68 |
| - // create and link next node |
69 |
| - RandomListNode currNode = head; |
70 |
| - while ( currNode != null ) |
71 |
| - { |
72 |
| - RandomListNode nextNode = currNode.next; |
73 |
| - |
74 |
| - RandomListNode copiedNode = new RandomListNode( currNode.label ); |
75 |
| - currNode.next = copiedNode; |
76 |
| - copiedNode.next = nextNode; |
77 |
| - |
78 |
| - currNode = nextNode; |
79 |
| - } |
80 |
| - |
81 |
| - // assign random pointer |
82 |
| - currNode = head; |
83 |
| - while ( currNode != null ) |
84 |
| - { |
85 |
| - RandomListNode nextNode = currNode.next.next; |
86 |
| - |
87 |
| - currNode.next.random = currNode.random == null ? null : currNode.random.next; |
88 |
| - |
89 |
| - currNode = nextNode; |
90 |
| - } |
91 |
| - |
92 |
| - // assign next pointer |
93 |
| - RandomListNode copiedHead = head.next; |
94 |
| - currNode = head; |
95 |
| - RandomListNode currCopiedNode = copiedHead; |
96 |
| - while ( currNode != null ) |
97 |
| - { |
98 |
| - RandomListNode nextNode = currCopiedNode.next; |
99 |
| - RandomListNode nextCopiedNode = nextNode == null ? null : nextNode.next; |
100 |
| - |
101 |
| - currNode.next = nextNode; |
102 |
| - currCopiedNode.next = nextCopiedNode; |
103 |
| - |
104 |
| - currNode = nextNode; |
105 |
| - currCopiedNode = nextCopiedNode; |
106 |
| - } |
107 |
| - |
108 |
| - return copiedHead; |
109 |
| - } |
110 |
| - |
111 |
| - @Test |
112 |
| - public void test() |
113 |
| - { |
114 |
| - RandomListNode node = new RandomListNode( 1 ); |
115 |
| - RandomListNode copiedNode = copyRandomList( node ); |
116 |
| - } |
117 |
| -} |
| 1 | +package hashtable; |
| 2 | + |
| 3 | +import java.util.HashMap; |
| 4 | +import java.util.Map; |
| 5 | + |
| 6 | +import org.junit.Test; |
| 7 | + |
| 8 | +// Definition for singly-linked list with a random pointer. |
| 9 | + class RandomListNode |
| 10 | + { |
| 11 | + int label; |
| 12 | + RandomListNode next, random; |
| 13 | + RandomListNode(int x) { this.label = x; } |
| 14 | + }; |
| 15 | + |
| 16 | +public class CopyListWithRandomPointer |
| 17 | +{ |
| 18 | + public RandomListNode copyRandomListHashMap( RandomListNode head ) |
| 19 | + { |
| 20 | + if ( head == null ) |
| 21 | + { |
| 22 | + return null; |
| 23 | + } |
| 24 | + |
| 25 | + Map<RandomListNode, RandomListNode> map = new HashMap<RandomListNode, RandomListNode>(); |
| 26 | + RandomListNode dummyHead = new RandomListNode(0); |
| 27 | + RandomListNode resultTail = dummyHead, newNode; |
| 28 | + RandomListNode currNode = head; |
| 29 | + while ( currNode != null ) |
| 30 | + { |
| 31 | + // create node |
| 32 | + map.putIfAbsent( currNode, new RandomListNode( currNode.label ) ); |
| 33 | + newNode = map.get( currNode ); |
| 34 | + |
| 35 | + // assign random pointer |
| 36 | + if ( currNode.random != null ) |
| 37 | + { |
| 38 | + map.putIfAbsent( currNode.random, new RandomListNode( currNode.label ) ); |
| 39 | + newNode = map.get( currNode.random ); |
| 40 | + } |
| 41 | + |
| 42 | + // concatenate |
| 43 | + resultTail.next = newNode; |
| 44 | + resultTail = newNode; |
| 45 | + currNode = currNode.next; |
| 46 | + } |
| 47 | + |
| 48 | + return dummyHead.next; |
| 49 | + } |
| 50 | + |
| 51 | + public RandomListNode copyRandomList( RandomListNode head ) |
| 52 | + { |
| 53 | + if ( head == null ) |
| 54 | + { |
| 55 | + return null; |
| 56 | + } |
| 57 | + |
| 58 | + // create and link next node |
| 59 | + RandomListNode currNode = head; |
| 60 | + while ( currNode != null ) |
| 61 | + { |
| 62 | + RandomListNode nextNode = currNode.next; |
| 63 | + |
| 64 | + RandomListNode copiedNode = new RandomListNode( currNode.label ); |
| 65 | + currNode.next = copiedNode; |
| 66 | + copiedNode.next = nextNode; |
| 67 | + |
| 68 | + currNode = nextNode; |
| 69 | + } |
| 70 | + |
| 71 | + // assign random pointer |
| 72 | + currNode = head; |
| 73 | + while ( currNode != null ) |
| 74 | + { |
| 75 | + RandomListNode nextNode = currNode.next.next; |
| 76 | + |
| 77 | + currNode.next.random = currNode.random == null ? null : currNode.random.next; |
| 78 | + |
| 79 | + currNode = nextNode; |
| 80 | + } |
| 81 | + |
| 82 | + // assign next pointer |
| 83 | + RandomListNode copiedHead = head.next; |
| 84 | + currNode = head; |
| 85 | + RandomListNode currCopiedNode = copiedHead; |
| 86 | + while ( currNode != null ) |
| 87 | + { |
| 88 | + RandomListNode nextNode = currCopiedNode.next; |
| 89 | + RandomListNode nextCopiedNode = nextNode == null ? null : nextNode.next; |
| 90 | + |
| 91 | + currNode.next = nextNode; |
| 92 | + currCopiedNode.next = nextCopiedNode; |
| 93 | + |
| 94 | + currNode = nextNode; |
| 95 | + currCopiedNode = nextCopiedNode; |
| 96 | + } |
| 97 | + |
| 98 | + return copiedHead; |
| 99 | + } |
| 100 | + |
| 101 | + @Test |
| 102 | + public void test() |
| 103 | + { |
| 104 | + RandomListNode node = new RandomListNode( 1 ); |
| 105 | + RandomListNode copiedNode = copyRandomList( node ); |
| 106 | + } |
| 107 | +} |
0 commit comments