-
Notifications
You must be signed in to change notification settings - Fork 2
/
CopyListWithRandomPointer.java
127 lines (110 loc) · 3.45 KB
/
CopyListWithRandomPointer.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
package Leetcode;
/**
* @author kalpak
*
* A linked list is given such that each node contains an additional random pointer which could point to any node in the list or null.
*
* Return a deep copy of the list.
*
* The Linked List is represented in the input/output as a list of n nodes. Each node is represented as a pair of [val, random_index] where:
*
* val: an integer representing Node.val
* random_index: the index of the node (range from 0 to n-1) where random pointer points to, or null if it does not point to any node.
*
*
* Example 1:
*
*
* Input: head = [[7,null],[13,0],[11,4],[10,2],[1,0]]
* Output: [[7,null],[13,0],[11,4],[10,2],[1,0]]
* Example 2:
*
*
* Input: head = [[1,1],[2,1]]
* Output: [[1,1],[2,1]]
* Example 3:
*
*
*
* Input: head = [[3,null],[3,0],[3,null]]
* Output: [[3,null],[3,0],[3,null]]
* Example 4:
*
* Input: head = []
* Output: []
* Explanation: Given linked list is empty (null pointer), so return null.
*
*
* Constraints:
*
* -10000 <= Node.val <= 10000
* Node.random is null or pointing to a node in the linked list.
* The number of nodes will not exceed 1000.
*
*/
class NodeWithRandomPointer {
int val;
NodeWithRandomPointer next;
NodeWithRandomPointer random;
public NodeWithRandomPointer(int val) {
this.val = val;
this.next = null;
this.random = null;
}
}
public class CopyListWithRandomPointer {
public static NodeWithRandomPointer copyRandomList(NodeWithRandomPointer head) {
if(head == null)
return head;
NodeWithRandomPointer current = head;
// copy the nodes
while(current != null) {
NodeWithRandomPointer newNode = new NodeWithRandomPointer(current.val);
newNode.next = current.next;
current.next = newNode;
current = current.next.next;
}
// copy the random pointers to the new nodes
current = head;
while(current != null) {
if(current.random != null)
current.next.random = current.random.next;
else
current.next.random = null;
current = current.next.next;
}
// Split the lists
NodeWithRandomPointer l1 = head;
NodeWithRandomPointer l2 = head.next;
NodeWithRandomPointer clonedHead = head.next;
while(l1 != null) {
l1.next = l1.next.next;
if(l2.next != null)
l2.next = l2.next.next;
else
l2.next = null;
l1 = l1.next;
l2 = l2.next;
}
return clonedHead;
}
public static void main(String[] args) {
NodeWithRandomPointer head = new NodeWithRandomPointer(7);
head.next = new NodeWithRandomPointer(13);
head.next.next = new NodeWithRandomPointer(11);
head.next.next.next = new NodeWithRandomPointer(10);
head.next.next.next.next = new NodeWithRandomPointer(1);
head.next.random = head;
head.next.next.random = head.next.next.next.next;
head.next.next.next.random = head.next.next;
head.next.next.next.next.random = head;
NodeWithRandomPointer result = copyRandomList(head);
while(result != null) {
System.out.print(result.val + " ");
if(result.random != null)
System.out.print("Random: "+result.random.val);
System.out.println();
result = result.next;
}
}
}