//题目描述
<?php
/*class RandomListNode{
var $label;
var $next = NULL;
var $random = NULL;
function __construct($x){
$this->label = $x;
}
}*///第一种思路
function MyClone($pHead){
if($pHead==null)
return ;
$pnode=new RandomListNode($pHead->label);
$pnode->next=$pHead->next;
$pnode->random=$pHead->random;
$pnode->next=MyClone($pHead->next);
return $pnode;
}//第二种思路
function MyClone($pHead){
//1.将A->B->C 变为A->A1->B->B1->C->C1
$cu=$pHead;
while ($cu!=null) {
$new = new RandomListNode($cu->label);
$new->next=$cu->next;
$cu->next=$new;
$cu=$new->next;
}
//2.如果原始链表上的节点N的random指向S,则对应的复制节点N'的random指向S的下一个节点S'
$cu=$pHead;
while ($cu!=null) {
$cu->next->random=$cu->random->next;
$cu=$cu->next->next;
}
//3.拆分
$head=$pHead->next;
$cu=$head;
while ($cu!=null) {
$cu->next=$cu->next->next;
$cu=$cu->next;
}
return $head;
}
625



被折叠的 条评论
为什么被折叠?



