Prove safety offer: complex list of replication (java)

Title: Implement a complex function ComplexListNode copy list. Complex linked list, each node has a m_pNext addition to a pointer to the next node, there is a point m_pSibling any node in the linked list or NULL;

   Below is a containing 5 of the complex type of list nodes. The solid line arrows indicate m_pNext pointer dashed arrows m_pSibling pointer. For simplicity, point to NULL pointer is not shown.

                  Featured programmers face questions 100 questions (49) - a complex chain of copy - Haitao - Microsoft, Google and other face questions

    See the problem, my first reaction was divided into two steps: The first step is to copy each list on the original list, and with m_pNext linked. The second step, assuming that the original list of a node N is m_pSibling point node S . Since the S position is possible on the list in N preceding may in N behind, so to target N position to find we need to start from the head of the linked list of the original node. Suppose the original node from the head of the linked list has elapsed s step to find the node S . Then copied nodes in the list N of m_pSibling of S ' , the distance from the head of the list copied node is S . We will be able to set up this way for each node on the copy list m_pSibling up.

          Comprising a pair of n linked list of nodes, each node since the positioning m_pSibling , have elapsed from the head of the list needs to node O (n) steps to find, so the total time complexity of this method is O (n 2 ) .

           Since the time of the method is mainly spent on positioning node m_pSibling above, we try to do optimization in this regard. We still divided into two steps: The first step is to copy each node is still on the original list N , and creates N ' , then create out of these nodes are linked. Here we <N , N '> into the pair information in a hash table. The second step is arranged to copy each node on the list m_pSibling . If the node in the linked list of the original N of m_pSibling point to the node S , then copy the list, the corresponding N ' should point S' . Thanks to the hash table, we can use the O (1) time in accordance with S find S ' .

           A second method corresponds to a space for time to O (n) space consumption achieved O (n) time efficiency.

    Next we back to an idea, to achieve O (n) time efficiency without the use of auxiliary space. The first step in the third method is still to create N corresponding to each node N in accordance with the original list ' . This time, we put N 'link to the following N.

    The second step m_pSibling copy out node. N is assumed that the list on the original point m_pSibling node S, then the corresponding copy out N 'is m_pnext point node, the same S' is S, m_pNext point node.

    The third step is to split this long list into two lists: the junction with the odd positions is linked m_pnext initial list, the even positions of the nodes linked by m_pnext is copied out of the list. 

public class Solution {
    public ComplexListNode Clone(ComplexListNode pHead)
    {
        if(pHead == null){
            return null;
        }
        //复制接结点
        CloneNodes(pHead);

        //连接随机指针
        ConnectSiblingNodes(pHead);

        //拆分链表
        return ReconnectNodes(pHead);

    }
    public static void CloneNodes(ComplexListNode pHead){
        ComplexListNode pNode = pHead;
        while(pNode != null){
            ComplexListNode pCloned = new ComplexListNode(pNode.label);
            pCloned.m_pnext = pNode.m_pnext;
            pCloned.m_pSibling = null;

            pNode.m_pnext = pCloned;
            pNode = pCloned.m_pnext;
        }


    }
    public static void ConnectSiblingNodes(ComplexListNode pHead){
        RandomListNode pNode = pHead;
        while(pNode != null){
            ComplexListNode pCloned = pNode.m_pnext;
            if(pNode.m_pSibling != null){
                pCloned.m_pSibling = pNode.m_Sibling.m_pnext;
            }

            pNode = pCloned.m_pnext;
        }
    }
    public static ComplexListNode ReconnectNodes(ComplexListNode pHead){

        ComplexListNode pNode = pHead;
        ComplexListNode pClonedHead = null;
        ComplexListNode pClonedNode = null;

        if(pNode != null){
            pClonedHead = pClonedNode = pNode.m_pnext;
            pNode.m_pnext = pClonedNode.m_pnext;
            pNode = pNode.m_pnext;
        }

        while(pNode != null){
            pClonedNode.m_next = pNode.m_next;
            pClonedNode = pClonedNode.m_pnext;
            pNode.m_pnext = pClonedNode.m_pnext;
            pNode = pNode.m_pnext;
        }

        return pClonedHead;
    }
}




Published 118 original articles · won praise 35 · views 120 000 +

Guess you like

Origin blog.csdn.net/abc7845129630/article/details/52729390
Recommended