-
Notifications
You must be signed in to change notification settings - Fork 79
/
Copy pathSolution.java
91 lines (74 loc) · 2.22 KB
/
Solution.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
package thirteen;
/**
* @author dmrfcoder
* @date 2019-04-19
*/
/**
* Definition for singly-linked list with a random pointer.
*/
class RandomListNode {
int label;
RandomListNode next, random;
RandomListNode(int x) {
this.label = x;
}
};
public class Solution {
public RandomListNode copyRandomList(RandomListNode head) {
if (head == null) {
return null;
}
RandomListNode curNode = head;
RandomListNode curNodeNext;
while (curNode != null) {
RandomListNode copyOfCurNode = new RandomListNode(curNode.label);
curNodeNext = curNode.next;
curNode.next = copyOfCurNode;
copyOfCurNode.next = curNodeNext;
curNode = curNode.next.next;
}
curNode = head;
while (curNode != null) {
curNode.next.random = curNode.random;
curNode = curNode.next.next;
}
RandomListNode copyListHead = head.next;
RandomListNode curNewNode = head.next;
RandomListNode tempNode;
curNode = head;
while (curNode != null) {
tempNode = curNewNode.next;
curNode.next = tempNode;
if (tempNode != null) {
curNewNode.next = tempNode.next;
} else {
curNewNode.next = null;
}
curNode = tempNode;
if (curNode!=null){
curNewNode = curNode.next;
}
}
return copyListHead;
}
public static void main(String[] args) {
RandomListNode head = new RandomListNode(-1);
RandomListNode node2 = new RandomListNode(1);
RandomListNode node3 = new RandomListNode(3);
RandomListNode node4 = new RandomListNode(4);
RandomListNode node5 = new RandomListNode(5);
head.next = node2;
//node2.next = node3;
// node3.next = node4;
// node4.next = node5;
//
// head.random = node5;
// node2.random = head;
// node3.random = null;
// node4.random = node3;
// node5.random = node2;
Solution s = new Solution();
RandomListNode randomListNode = s.copyRandomList(head);
int a = 0;
}
}