[List] list of replication complex

Disclaimer: This article is a blogger original article, shall not be reproduced without the bloggers allowed. https://blog.csdn.net/m0_38033475/article/details/91346998

The general idea:

At first thought it was to create a new list, one by one copy by next relationship, copy good again tube random. The random pointer is a problem, because it is difficult to know the "original linked list points to a node" is a node should be the new list in which nodes feel the need to find a correspondence between nodes , and the knot point value and is not necessarily different, it is only through "the first few" come to such a number that still have to traverse one by one thing, it is clear that this time complexity is O (n ^ 2)

After suddenly see explanations, the original correspondence relationship between such nodes, may be made entirely " inserted adjacent " to represent it! So the idea came:

1、复制每个节点,如:复制节点A得到A1,将A1插入节点A后面

2、遍历链表,A1->random = A->random->next;

3、将链表拆分成原链表和复制后的链表

In this case, assign random random pointer can see the original node, as long as the next it will be able to point to the appropriate node copy it! After processing the list just complexity O (n)

But, before insertion and are processed on the original list, make sure the final list of new and original list "Split." Split, so long as you understand very well written, to be understood. As shown before and after the split case:

AC Code:

/*
struct RandomListNode {
    int label;
    struct RandomListNode *next, *random;
    RandomListNode(int x) :
            label(x), next(NULL), random(NULL),index(0) {
    }
};
*/
class Solution {
public:
    

    RandomListNode* Clone(RandomListNode* pHead)
    {
        //判空
        if(pHead==NULL)
            return NULL;
        RandomListNode* h =new RandomListNode(pHead->label);
        h->next=pHead->next;
        h->random=pHead->random;
        //一个
        if(pHead->next==NULL)
            return h;
        
        RandomListNode* p = pHead;
        while(p!=NULL) //插入复制的新节点到原结点的下一个
        {
            RandomListNode* a = p;
            p = p->next;
            
            RandomListNode* b = new RandomListNode(0);
            b->label = a->label;
            b->next = a->next;
            a->next = b;   
        }
        //建好了链表之后,就可以对random进行复制啦
        p = pHead;
        while(p!=NULL && p->next!=NULL) //如果该结点有副本的话
        {
            RandomListNode* a = p; //原结点
            p=p->next->next;
            
            if(a->random!=NULL) //这里也要判断一下,避免指针为空还去调用->next! 
            {
            	RandomListNode* b = a->next; //副本
            	b->random = a->random->next; //副本的random也得是相应的副本!!!!!
			} 
        }
        //拆分
        RandomListNode *pCloneHead = pHead->next;
        RandomListNode *tmp;
        RandomListNode *currNode = pHead;
        while(currNode->next){  //需要好好理解掌握!
            tmp = currNode->next;
            currNode->next =tmp->next;
            currNode = tmp;
        }
        return pCloneHead;
    }
};

 

Guess you like

Origin blog.csdn.net/m0_38033475/article/details/91346998