forked from EasonLeeee/TargetOffer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patht26_复杂链表的复制.java
53 lines (46 loc) · 1.52 KB
/
t26_复杂链表的复制.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
/**
* 输入一个复杂链表(每个节点中有节点值,以及两个指针,一个指向下一个节点,另一个特殊指针指向任意一个节点),返回结果为复制后复杂链表的head。(注意,输出结果中请不要返回参数中的节点引用,否则判题程序会直接返回空)
*/
/**
* 思路:将新创建的节点添加到原节点的后面,最后一步分离。时间复杂度O(n);
* 特殊输入测试:1、头结点指向null 2.只有一个节点
*/
public class Solution {
public RandomListNode Clone(RandomListNode pHead)
{
if (pHead == null) {
return null;
}
//将拷贝的节点添加到原节点的后面
RandomListNode pCur = pHead;
while (pCur != null) {
RandomListNode newNode = new RandomListNode(pCur.label);
RandomListNode last = pCur;
pCur = pCur.next;
newNode.next = last.next;
last.next = newNode;
}
//进行random指针的拷贝
pCur = pHead;
while (pCur != null) {
if (pCur.random != null) {
pCur.next.random = pCur.random.next;
}
pCur = pCur.next.next;
}
//将复制的节点从原链表中分离
pCur = pHead;
RandomListNode pNewHead = pCur.next;
RandomListNode pNewCur = pNewHead;
while (pCur != null) {
pCur.next = pNewCur.next;
if (pNewCur.next == null) {
return pNewHead;
}
pNewCur.next = pNewCur.next.next;
pCur = pCur.next;
pNewCur = pNewCur.next;
}
return pNewHead;
}
}