-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathCopyListwithRandomPointer.java
More file actions
96 lines (73 loc) · 2.16 KB
/
CopyListwithRandomPointer.java
File metadata and controls
96 lines (73 loc) · 2.16 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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
/*CopyListwithRandomPointer.java
Copy List with Random Pointer
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.
Challenge Could you solve it with O(1) space?
Tags Hash Table Linked List Uber
*/
public class CopyListwithRandomPointer {
/**
* @param head: The head of linked list with a random pointer.
* @return: A new head of a deep copy of the list.
*/
private static class RandomListNode {
int label;
RandomListNode next, random;
RandomListNode(int x) { this.label = x; }
};
public RandomListNode copyRandomList(RandomListNode head) {
// write your code here
if (head == null) {
return head;
}
copyNext(head);
copyRandom(head);
return split(head);
}
private void copyNext(RandomListNode head) {
RandomListNode newHead = head;
while (head != null && head.next != null) {
newHead.next = head.next;
head.next = newHead;
head = newHead.next;
newHead = head;
}
if (head != null) {
newHead.next = head.next;
head.next = newHead;
}
}
private void copyRandom(RandomListNode head) {
while (head != null && head.next != null) {
head.next.random = head.random;
head = head.next.next;
}
}
private RandomListNode split(RandomListNode head) {
RandomListNode originList = head;
RandomListNode copiedList = head.next;
RandomListNode dummyOrigin = new RandomListNode(0);
RandomListNode dummyCopied = new RandomListNode(0);
dummyOrigin.next = originList;
dummyCopied.next = copiedList;
while (head != null && head.next != null) {
RandomListNode tmp = copiedList.next;
originList.next = tmp;
copiedList.next = tmp.next;
originList = originList.next;
copiedList = copiedList.next;
}
return dummyCopied.next;
}
}
/*Wrong Answer
Total Runtime: 137 ms
0% test cases passed.
Input
-1->null, [null]
Output
Node with label -1 was not copied but a reference to the original one.
Expected
-1->null, [null]
+-+
*/