/*
public class RandomListNode {
int label;
RandomListNode next = null;
RandomListNode random = null;
RandomListNode(int label) {
this.label = label;
}
}
*/
// 不难,遍历即可,注意判断先锋节点是否为空,时空复杂度均为O(n)
public class Solution {
public RandomListNode Clone(RandomListNode pHead) {
// 先判断头结点
if(pHead == null)
return null;
RandomListNode nodec = new RandomListNode(pHead.label);
RandomListNode node0 = nodec;
for(RandomListNode node = pHead; node != null; node = node.next){
// 访问一个节点的属性,一定要先保证这个节点不是空的
if(node.next != null) {
RandomListNode nodec_next = new RandomListNode(node.next.label);
nodec.next = nodec_next;
}
else
nodec.next = null;
if(node.random != null) {
RandomListNode nodec_rand = new RandomListNode(node.random.label);
nodec.random = nodec_rand;
}
else
nodec.random = null;
nodec = nodec.next;
}
return node0;
}
}