/*
public class RandomListNode {
int label;
RandomListNode next = null;
RandomListNode random = null;
RandomListNode(int label) {
this.label = label;
}
}
*/
public class Solution {
public RandomListNode Clone(RandomListNode pHead)
{
copyNodes(pHead);
setClonedNodes(pHead);
return splitNodes(pHead);
}
//第一步,复制链表任意结点N并创建新结点N‘,再把N'链接到N的后面
public static void copyNodes(RandomListNode head){
RandomListNode temp = head;
while(temp!=null){
RandomListNode clonedNode = new RandomListNode(0);
clonedNode.next = temp.next;
clonedNode.label = temp.label;
clonedNode.random = null;
temp.next = clonedNode;
temp = clonedNode.next;
}
}
//第二步,设置复制出来的结点
public static void setClonedNodes(RandomListNode head){
RandomListNode pNode = head;
while(pNode!=null){
RandomListNode pCloned = pNode.next;
if(pNode.random!=null){
pCloned.random = pNode.random.next;
}
pNode = pCloned.next;
}
}
//第三步,将第二步得到的链表拆分成两个链表
public static RandomListNode splitNodes(RandomListNode head){
RandomListNode pNode = head;
RandomListNode clonedHead = null;
RandomListNode clonedNode = null;
if(pNode!=null){
clonedHead = pNode.next;
clonedNode = pNode.next;
pNode.next = clonedNode.next;
pNode = pNode.next;
}
while(pNode!=null){
clonedNode.next = pNode.next;
clonedNode = clonedNode.next;
pNode.next = clonedNode.next;
pNode = pNode.next;
}
return clonedHead;
}
}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有