-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCopyRandomList.java
More file actions
51 lines (47 loc) · 1.42 KB
/
Copy pathCopyRandomList.java
File metadata and controls
51 lines (47 loc) · 1.42 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
package linkedList;
import org.junit.Test;
/**
* @Author: wei1
* @Date: Create in 2018/11/16 1:21
* @Description: 剑指offer复制链表
* 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.
* <p>
* Return a deep copy of the list.
* <p>
* 先拷贝原链表,在拷贝随机指针,在分离链表
*/
public class CopyRandomList {
public RandomListNode copyRandomList(RandomListNode head) {
//拷贝原链表
RandomListNode copy, i;
if (head == null) {
return null;
}
for (i = head; i != null; ) {
copy = new RandomListNode(i.label);
copy.next = i.next;
i.next = copy;
i = copy.next;
}
//拷贝随机指针
for (i = head; i != null; ) {
copy = i.next;
copy.random = (i.random == null ? null : i.random.next);
i = copy.next;
}
//分离链表
for (i = head, head = i.next; i != null; ) {
copy = i.next;
i = i.next = copy.next;
copy.next = (i == null ? null : i.next);
}
return head;
}
@Test
public void test() {
RandomListNode randomListNode = new RandomListNode(-1);
RandomListNode randomListNode1 = copyRandomList(randomListNode);
System.out.println(randomListNode1.label);
}
}