解题思路:思路:第一步在原链表的基础上复制节点,将节点复制在原节点的后面。第二步复制随机节点。 第三步将新旧链表分离。
代码如下:
class Conplex_List: def __init__(self,x,y=None,z=None): self.value=x self.next=y self.child=z def copy_listnode(root): if root ==None: return phead=root #复制新链表节点 while phead: listnode=Conplex_List(phead.value) listnode.next=phead.next phead.next=listnode phead=listnode.next #复制新链表节点的孩子节点 phead1=phead while phead1: if phead1.child: phead1.next.child=phead1.child.next phead1=phead1.next.next
#分离新旧列表 old=phead new=old.next while old: old.next=old.next.next if new.next: new=new.next.next old=old.next new=new.next return new