35 复杂链表的复制

package sort;

class ComliexListNode{
    
    int value;
    ComliexListNode next;
    ComliexListNode sibling;
    public int getValue() {
        return value;
    }
    public void setValue(int value) {
        this.value = value;
    }
    public ComliexListNode getNext() {
        return next;
    }
    public void setNext(ComliexListNode next) {
        this.next = next;
    }
    public ComliexListNode getSibling() {
        return sibling;
    }
    public void setSibling(ComliexListNode sibling) {
        this.sibling = sibling;
    }
    
    
}


public class Test35 {
    
    public static void main(String[] args) {
        ComliexListNode head=new ComliexListNode();
        head.value=10;
        head.sibling=head;
        CloneNodes(head);
        ConnSiblingNodes(head);
        ReconnectNode(head);
    }
    
    
    private static ComliexListNode ReconnectNode(ComliexListNode head) {
        //ComliexListNode point=null;
        //ComliexListNode phead=null;
        ComliexListNode pclone=null;
        ComliexListNode cloneHead=null;
        // TODO Auto-generated method stub
        if(head!=null){
        
    
        
         pclone=head.next;
         cloneHead=pclone;
        }
        
        while(pclone.next!=null){
        pclone.next=pclone.next.next;
        pclone=pclone.next;
            
            
            
        }
        
        return cloneHead;
    }


    private static void ConnSiblingNodes(ComliexListNode head) {
        // TODO Auto-generated method stub
        ComliexListNode temp1=head;
        ComliexListNode temp2=head.next;
        while (temp1!=null&&temp2!=null) {
            temp2.sibling=temp1.sibling.next;
            temp2=temp1.next;
            if(temp2!=null) temp1=temp2.next;
            
            
            
        }
    }


    public static void CloneNodes(ComliexListNode head){//复制节点、相当于在每个节点后添加一个复制节点
                                              //并用指针相连
        ComliexListNode point=head;
        while (point!=null) {
            ComliexListNode temp=new ComliexListNode();
            temp.value=point.value;
            temp.next=point.next;
            point.next=temp;
            
            point=point.next.next;
            
        }
        
        
    }

}
 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值