剑指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);

    }


}

猜你喜欢

转载自blog.csdn.net/ma451152002/article/details/83212360