-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathListWithRandomPointer.java
More file actions
76 lines (60 loc) · 1.76 KB
/
ListWithRandomPointer.java
File metadata and controls
76 lines (60 loc) · 1.76 KB
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
package LinkedLists;
public class ListWithRandomPointer {
public static ListNode copyRandomList(ListNode head) {
ListNode head2 = new ListNode(0);
ListNode temp2 = head2;
ListNode temp1 = head;
// Creating Deep Copy.
while (temp1 != null) {
temp2.next = new ListNode(temp1.val);
temp1 = temp1.next;
temp2 = temp2.next;
}
head2 = head2.next;
temp2 = head2;
temp1 = head;
ListNode temp = new ListNode(-1);
// Alternate Connections.
while (temp1 != null) {
temp.next = temp1;
temp1 = temp1.next;
temp = temp.next;
temp.next = temp2;
temp2 = temp2.next;
temp = temp.next;
}
temp2 = head2;
temp1 = head;
// Assigning Random Pointers.
while (temp1 != null && temp2 != null) {
if (temp1.random == null) temp2.random = null;
else temp2.random = temp1.random.next;
temp1 = temp2.next;
if (temp1 != null) temp2 = temp1.next;
}
temp2 = head2;
temp1 = head;
// Separating Between the Lines.
while (temp1 != null) {
temp1.next = temp2.next;
temp1 = temp1.next;
if (temp1 == null) break;
temp2.next = temp1.next;
if (temp2.next == null) break;
temp2 = temp2.next;
}
return head2;
}
public static void main(String[] args) {
}
static class ListNode {
int val;
ListNode next;
ListNode random;
public ListNode(int val) {
this.val = val;
this.next = null;
this.random = null;
}
}
}