-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCopyComplexList.java
62 lines (55 loc) · 1.13 KB
/
CopyComplexList.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
package leetcode;
/**
* 复杂链表的复制
*
* @author HJH
*
*/
public class CopyComplexList {
// 复杂链表的定义
class RandomListNode {
int label;
RandomListNode next, random;
RandomListNode(int x) {
this.label = x;
}
};
public RandomListNode copyRandomList(RandomListNode head) {
if (head==null) {
return null;
}
RandomListNode cur = head;
RandomListNode next = null;
//复制链表结点,并连接在当前结点的后面
while(cur!=null){
next = cur.next;
cur.next = new RandomListNode(cur.label);
cur.next.next = next;
cur = next;
}
cur = head;
next = null;
//获取复制后链表结点的random结点
while(cur!=null){
next = cur.next.next;
cur.next.random = cur.random!=null ? cur.random.next : null;
cur = next;
}
//拆分链表
cur = head;
next = null;
RandomListNode copyHead = head.next;
RandomListNode curCopy;
while(cur!=null){
next = cur.next.next;
curCopy = cur.next;
cur.next = next;
curCopy.next = curCopy.next!=null ? curCopy.next.next : null;
cur = next;
}
return copyHead;
}
public static void main(String[] args) {
// TODO Auto-generated method stub
}
}