剑指offer-问题26

package offer;

/**
 * offer interview 26
 */
public class Test26 {

    public static class ComplexListNode{
        int value;
        ComplexListNode next;
        ComplexListNode sibling;

        public ComplexListNode(){}
        public ComplexListNode(int value){
            this.value = value;
            this.next = null;
            this.sibling = null;
        }
    }

    public static ComplexListNode clone(ComplexListNode head){
        if (head == null){
            return null;
        }

        cloneNodes(head);
        connectNodes(head);
        return reconnectNodes(head);
    }

    public static void cloneNodes(ComplexListNode head){
        while (head != null){
            ComplexListNode tmp = new ComplexListNode();
            tmp.value = head.value;

            tmp.next = head.next;
            head.next = tmp;

            head = tmp.next;
        }
    }

    public static void connectNodes(ComplexListNode head){
        while (head != null){
            if (head.sibling != null){
                head.next.sibling = head.sibling.next;
            }
            head = head.next.next;
        }
    }

    public static ComplexListNode reconnectNodes(ComplexListNode head){
        if (head == null){
            return null;
        }

        ComplexListNode newHead = head.next;
        ComplexListNode pointer = newHead;
        head.next = newHead.next;
        head = head.next;

        while (head != null){
            pointer.next = head.next;
            pointer = pointer.next;
            head.next = pointer.next;
            head = pointer.next;
        }
        return newHead;
    }

    public static void printList(ComplexListNode head){
        while (head != null){
            System.out.print(head.value + "->");
            head = head.next;
        }
        System.out.println("null");
    }


    public static boolean isSame(ComplexListNode h1,ComplexListNode h2){
        while (h1 != null && h2 != null){
            if (h1 == h2){
                h1 = h1.next;
                h2 = h2.next;
            }else{
                return false;
            }
        }

        return h1 == null && h2 == null;
    }

    public static void main(String[] args){
        //          -----------------
        //         \|/              |
        //  1-------2-------3-------4-------5
        //  |       |      /|\             /|\
        //  --------+--------               |
        //          -------------------------
        ComplexListNode head = new ComplexListNode();
        head.value = 1;
        head.next = new ComplexListNode();
        head.next.value = 2;
        head.next.next = new ComplexListNode();
        head.next.next.value = 3;
        head.next.next.next = new ComplexListNode();
        head.next.next.next.value = 4;
        head.next.next.next.next = new ComplexListNode();
        head.next.next.next.next.value = 5;

        head.sibling = head.next.next;
        head.next.sibling = head.next.next.next.next.next;
        head.next.next.next.sibling = head.next;

        ComplexListNode tmp = head;
        printList(tmp);

        ComplexListNode newHead = clone(head);
        printList(head);
        printList(newHead);
        System.out.println(isSame(head,tmp));
        System.out.println(isSame(head,newHead));


        // 有指向自身的情况
        //          -----------------
        //         \|/              |
        //  1-------2-------3-------4-------5
        //         |       | /|\           /|\
        //         |       | --             |
        //         |------------------------|
        ComplexListNode head2 = new ComplexListNode();
        head2.value = 1;
        head2.next = new ComplexListNode();
        head2.next.value = 2;
        head2.next.next = new ComplexListNode();
        head2.next.next.value = 3;
        head2.next.next.next = new ComplexListNode();
        head2.next.next.next.value = 4;
        head2.next.next.next.next = new ComplexListNode();
        head2.next.next.next.next.value = 5;

        head2.next.sibling = head2.next.next.next.next;
        head2.next.next.next.sibling = head2.next.sibling;
        head2.next.next.sibling = head2.next.next;

        System.out.println("\n");
        tmp = head2;
        printList(head2);
        ComplexListNode newHead2 = clone(head2);
        printList(head2);
        System.out.println(isSame(head2, tmp));
        printList(newHead2);
        System.out.println(isSame(head2, newHead2));

        ComplexListNode head3 = new ComplexListNode();
        head3.value = 1;

        System.out.println("\n");
        tmp = head3;
        printList(head3);
        ComplexListNode newHead3 = clone(head3);
        printList(head3);
        System.out.println(isSame(head3, tmp));
        printList(newHead3);
        System.out.println(isSame(head3, newHead3));

        System.out.println("\n");
        ComplexListNode head4 = clone(null);
        printList(head4);

    }


}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值