Java implements replication of complex linked lists

Input a complex linked list (there is a node value in each node, and two pointers, one points to the next node, and the other special pointer points to any node), the return result is the head of the complex linked list after copying

code

    static class ListNode {
        int val;
        ListNode next;
        ListNode other;

        public ListNode(int val) {
            this.val = val;
        }

        @Override
        public String toString() {
            return "ListNode{" +
                    "val=" + val +
                    ", next=" + (next == null ? "null" : next.val) +
                    ", other=" + (other == null ? "null" : other.val) +
                    "}";
        }
    }

    /**
     * copy
     * The original linked list is a->b->c
     * After copying, it is a->a1->b->b1->c->c1
     * @param head
     */
    private static void copy(ListNode head) {
        ListNode listNode = head;
        while (listNode != null) {
            // Create a new node based on the content of the current node
            ListNode copy = new ListNode(listNode.val);
            copy.next = listNode.next;

            // Use the new node as the next node of the current node, and the next node of the new node points to the original next node of the current node
            // The original is a->b, the selection is a->a1->b
            listNode.next = copy;
            // Point to the next node, repeat the above operation
            listNode = copy.next;
        }
    }

    /**
     * Set other node
     * @param head
     */
    private static void setOther(ListNode head) {
        ListNode listNode = head;
        // listNode is the original linked list node, copy is the corresponding copy node, while moving the pointer to traverse
        while (listNode != null) {
            // The next node of the current node is its copy node
            ListNode copy = listNode.next;

            // When the other node of the current node is not null, the other of the copied node should be the next node of the other node of the source node
            // a -> a1 -> b -> b1 a1 is the replica node of a, b1 is the replica node of b
            // | ↑ a.other is b, b.next node is b1 (guaranteed by the first copy)
            // |_ _ _ _ _ | So the other node of a1 is a.other, which is the next of b, which is a.other.next
            if (listNode.other != null) {
                copy.other = listNode.other.next;
            }
            // The current node points to the next node of its copy node, that is, the original next node of the source node, repeat the above operation
            listNode = copy.next;
        }
    }

    /**
     * Disconnect from the original linked list
     * @param head
     * @return
     */
    private static ListNode disConnect(ListNode head) {
        ListNode listNode = head;
        ListNode copyHead = null;
        ListNode copyNode = null;
        if (listNode != null) {
            // The head node of the copy linked list is the next node of the current linked list head node (ie its copy node)
            copyHead = listNode.next;
            // used to traverse the copy linked list
            copyNode = listNode.next;
            // The next of the original linked list node of the current linked list points to the original next (this time it is the next of its copy node)
            listNode.next = copyNode.next;
            // Move the pointer of the current linked list, this time is the head node of the copied linked list
            listNode = listNode.next;
        }

        while (listNode != null) {
            // Copy the next of the linked list node to the next of the current node
            // a -> a1 -> b -> b1,变回 a1 -> b1
            copyNode.next = listNode.next;
            // move the pointer to the copy list
            copyNode = copyNode.next;

            // Point the next of the node in the original linked list to the original next (this time it is the next of its copied node)
            // a -> a1 -> b -> b1,变回 a -> b
            listNode.next = copyNode.next;
            // Move the pointer of the current linked list, at this time the original linked list is traversed
            listNode = listNode.next;
        }

        return copyHead;
    }

    public static ListNode cloneNode(ListNode head) {
        if (head == null) {
            return null;
        }
        copy(head);
        setOther(head);
        return disConnect(head);
    }

    public static void main(String[] args) {
        ListNode root = bulidList();
        ListNode copy = cloneNode(root);
        while (root != null) {
            System.out.println(root.toString());
            root = root.next;
        }
        while (copy != null) {
            System.out.println(copy.toString());
            copy = copy.next;
        }
    }

    private static ListNode bulidList() {
        ListNode node1 = new ListNode(1);
        ListNode node2 = new ListNode(2);
        ListNode node3 = new ListNode(3);
        ListNode node4 = new ListNode(4);
        ListNode node5 = new ListNode(5);
        node1.next = node2;
        node2.next = node3;
        node3.next = node4;
        node4.next = node5;

        node1.other = node3;
        node4.other = node1;
        return node1;
    }

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325925576&siteId=291194637