Offer26: Replication of Complex Linked Lists

Posted by MickeySox on Mon, 02 Sep 2019 10:30:40 +0200

Replication of Complex Linked Lists

Problem Description

 * Title Description:
 * Please implement the function ComplexListNode (ComplexListNode pHead) to copy a complex linked list. In a complex linked list, each node except for one m_pNext pointer points to the next node
 * In addition, there is an m_pSibling pointing to any node in the list or NULL.

problem analysis

 * Problem analysis:
 * The most original solution: in two steps, the first step is to copy each node on the original list and link it with mPnext; the second step is to set the pSibling pointer of each node. Assuming that the original list
 * The pSibling of a node N points to the node S. Since the position of S may be in front of or behind N in the list, the location of S needs to be located from the head node of the original list. If from primitive
 * The head node of the list begins to find the node S along the S-step of pNext, so the distance between the node N's pSibling (S') on the replicated list and the head node of the replicated list is also along the pNext pointer s-step. This time complexity is
 *      o(n^2). 
 * The second method:
 * There are still two steps. The first step is to copy each node N on the original list to create N', and then link these created nodes with pNext. At the same time, we put the matching information of < N, N'> into a hash table.
 * The second step is to set up pSibling for each node on the replication list. If the mSibling of the node N in the original list points to the node S, then in the replicated list, the corresponding N'should point to the S'. Because of the hash table, we can use o(1)
 * Time to find S'according to S.
 * The third method is:
 * The first step is still to create the corresponding N', based on each node N of the original list. This time, link N'to the back of N.
 * The second step is to set up the duplicated node pSibling. Assuming that the pSibling of N on the original list points to the node S, then the corresponding duplicated N'is the node of N's pNext.
 * S'is also the point that S's pNext points to. Set the linked list after pSibling.
 * The third step is to divide the long list into two lists: linking the odd-numbered nodes with pNext is the position of the original list, and linking the even-numbered nodes with pNext is the duplicated list.
public class Code026 {
    public static ComplexListNode main(String[] args){
        ComplexListNode pHead=new ComplexListNode();
        CloneNodes(pHead);
        ConnectSiblingNodes(pHead);
        return ReconnectNodes(pHead);
    }
    private static void CloneNodes(ComplexListNode pHead){
        ComplexListNode pNode=pHead;
        while (pNode!=null){
            ComplexListNode pCloned=new ComplexListNode();
            pCloned.value=pNode.value;
            pCloned.pNext=pNode.pNext;
            pCloned.pSibling=null;
            pNode.pNext=pCloned;
            pNode=pCloned.pNext;
        }
    }
    private static void ConnectSiblingNodes(ComplexListNode pHead){
        ComplexListNode pNode=pHead;
        while (pNode!=null){
            ComplexListNode pCloned=pNode.pNext;
            if(pNode.pSibling!=null){
                pCloned.pSibling=pNode.pSibling.pNext;
            }
            pNode =pCloned.pNext;
        }
    }
    private static ComplexListNode ReconnectNodes(ComplexListNode pHead){
        ComplexListNode pNode=pHead;
        ComplexListNode pClonedHead=null;
        ComplexListNode pClonedNode=null;
        if(pNode!=null){
            pClonedHead=pClonedNode=pNode.pNext;
            pNode.pNext=pClonedNode.pNext;
            pNode=pNode.pNext;
        }
        while (pNode!=null){
            pClonedNode.pNext=pNode.pNext;
            pClonedNode=pClonedNode.pNext;
            pNode.pNext=pClonedNode.pNext;
            pNode=pNode.pNext;
        }
        return pClonedHead;
    }
    private static class ComplexListNode{
        int value;
        ComplexListNode pNext;
        ComplexListNode pSibling;
    }
}