Sword refers to offer interview question 26-copy of complex linked list, sword refers to offer

Source: Internet
Author: User

Sword refers to offer interview question 26-copy of complex linked list, sword refers to offer

    struct ComplexListNode      {          int m_nValue;          ComplexListNode* m_pNext;          ComplexListNode* m_pSibling;      };  


For a complex linked list with five nodes, the solid line indicates the point of the m_pNext pointer, And the dotted line indicates the point of the m_pSibling pointer.

Solution 1:

Two steps

1: traverse the linked list and use the m_pNext pointer to connect the linked list. O (n)

2: Assume that m_pSibling of node N in the original linked list points to node S, because S may be located before or behind N in the linked list, therefore, to locate N, you need to start from the head node of the original linked list. Assuming that the node s is found from the head node of the original linked list, then, the distance between m_pSibling's 'of node N on the replication linked list and the head node of the replication linked list is also S. In this way, we can set m_pSibling for each node on the replication linked list. For a linked list containing n nodes, the m_pSibling of each node must start from the head node of the linked list and go through the O (n) step, therefore, the total time complexity of this method is O (n2)

The time cost is O (n ^ 2), and the space cost is 0.


Solution 2:

M_pSibling pointing to a node in method 1 is less efficient. The space is used for time, which is also divided into two steps.

1: traverse the linked list and use the m_pNext pointer to connect the linked list. At the same time, create a hash ing between node N in the original linked list and the corresponding copy node n' <N, n'>

2: traverse the original linked list again. Find m' for each node m by hash and find the node m_pSibling of m in the original linked list, find the node to which the m_pSibling pointer of m' is directed, and modify the m_pSibling pointer of M '.

The time cost is O (n), and the Space Price is O (n)


Solution 3:

Clever, just traverse the linked list three times, the time cost is O (n), the space cost is 0, divided into three steps


1: traverse the original linked list, copy the N 'corresponding to node N, and insert it to the end of node N, as shown in


The Code is as follows:

    void CloneNodes(ComplexListNode* pHead)      {          ComplexListNode* p = pHead;          while ( p )          {              ComplexListNode* pCloned = new ComplexListNode;              pCloned->m_nValue = p->m_nValue;              pCloned->m_pNext = p->m_pNext;              p->m_pNext = pCloned;              pCloned->m_pSibling = NULL;                    p = p->m_pNext;          }      }  

2: determine the direction of each m_pSibling pointer. If the m_pSibling of N on the original linked list points to node S, the copied n' is the m-> pNext node of N, S is also the m-> pNext node of S,

This is why we copied the node link from each node to the original node in the previous step. With this connection method, we can find the m_pSibling of each node in O (1. Obtain the structure as shown in Figure


The Code is as follows:

Void ConnectSiblingNodes (ComplexListNode * pHead) {ComplexListNode * p = pHead; ComplexListNode * pCloned = NULL; // record the node m_pSibling to be determined currently while (p) {pCloned = p-> m_pNext; if (pCloned-> m_pSibling) pCloned-> m_pSibling = p-> m_pSibling-> m_pNext; p = pCloned-> m_pNext ;}}

3: traverse it again to separate the original linked list and the copy linked list. The odd number is the original linked list, and the even number is the copy linked list.


The Code is as follows:

ComplexListNode * Complete (ComplexListNode * pHead) {ComplexListNode * pNode = pHead; ComplexListNode * pClonedHead = NULL; // determine the returned header node ComplexListNode * pClonedNode = NULL; // special processing of the header node, boundary Condition judgment if (pNode! = NULL) {pClonedHead = pNode-> m_pNext; pClonedNode = pNode-> m_pNext; pNode-> m_pNext = pClonedHead-> m_pNext; pNode = pNode-> m_pNext;} while (pNode) {cursor-> m_pNext = pNode-> m_pNext; pNode-> m_pNext = cursor-> m_pNext; pClonedNode = pClonedNode-> m_pNext; pNode = pNode-> m_pNext;} return pClonedHead ;}

In the end, the three steps above are merged to replicate the whole process of the complex linked list:

    ComplexListNode* Clone(ComplexListNode* pHead)      {          CloneNodes(pHead);          ConnectSiblingNodes(pHead);                    return ReconnentNodes(pHead);      }  





Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.