24 linked list - replication of complex linked list

Posted by hermzz on Wed, 09 Oct 2019 16:05:59 +0200

Title Description

Enter a complex linked list (each node has a node value and two pointers, one pointing to the next node, and the other pointing to any node), and return the result as the head of the replicated complex linked list. (Note that in the output, do not return the node reference in the parameter, otherwise the judgement program will return to null directly)

struct RandomListNode {
    int label;
    struct RandomListNode *next, *random;
    RandomListNode(int x) :
            label(x), next(NULL), random(NULL) {
    }
};

Analysis:
New complex linked list, new node, assign the original value and address of the node in the original complex linked list to the new node, which is not right. The memory address of the original linked list is used, and the new allocated memory space is not used.

        RandomListNode* n = pHead;
        RandomListNode* *r = NULL;
        while(n){
            RandomListNode* body = new RandomListNode(n->label);
            body->next = n->next;
            body->random = n->random;//Assigning the original linked list address to the new linked list node, but using memory to restore the linked list is problematic.
            

Train of thought:

Reference resources Niu Ke LAN

Complex linked list replication schematic figure 1

1. Use the method of inserting nodes into complex linked list, such as inserting node A1 after node A and B1 after node B.

Copy the node A element value to A1

RandomListNode* node = pHead;
while(node){
    //Let the first node be A
    RandomListNode* nodeA1 = node;
    //Copy the element value of node A to the new node A1
    nodeA1->label = nodeA->label;
    //Assign the next pointer value of node A to node A1
    nodeA1->next = nodeA->next;
    //Point the next pointer of node A to node A1
    nodeA->next = nodeA1;
    //The next of the original list node points to A1, and the value of node needs to be updated.
    node = nodeA1->next;
}

2. After inserting complex linked list nodes into the same nodes and updating the next pointer field, we need to consider random pointer field.

According to the schematic diagram, A - > Random = B; C - > Random = B; A1 - > Random = B1; C1 - > Random = B1;

B->next = B1;C->next = C1;

The random pointer field of the newly inserted node points to the node, which is the random next pointer field of the original node.

A1->random = A->random ->next; 

		currNode = pHead;
		while(currNode){
			RandomListNode *node = currNode->next;
			if(currNode->random){
				node->random = currNode->random->next;
			}
			currNode = node->next;
		}

3. Separate the inserted nodes to form a separate linked list

    RandomListNode *pCloneHead = pHead->next;
    RandomListNode *tmp;
    currNode = pHead;
    while(currNode->next){
        //Setting variables to achieve currNode currNode - > next - > currNode = currNode - > next - > next
        tmp = currNode->next;
        currNode->next = tmp->next;
        //Update traversal node location
        currNode = tmp;
    }
    return pCloneHead ;

Reference code

class Solution{
public:
	RandomListNode *Clone(RandomListNode* pHead)
	{
		if(!pHead) return NULL;
		RandomListNode *currNode = pHead;//Assigning currNode to the address of the link head node as a traversal list
		while(currNode){
			RandomListNode *node = new RandomListNode(currNode->label);//Allocate memory space 
			node->next = currNode->next; //The currNode pointer points to the next node address and assigns it to the node 
			currNode->next = node; //Assign the value of node to currNode - > next? 
			currNode = node->next;
		} 
		currNode = pHead;
		while(currNode){
			RandomListNode *node = currNode->next;
			if(currNode->random){
				node->random = currNode->random->next;
			}
			currNode = node->next;
		}
		//split
		RandomListNode *pCloneHead = pHead->next;
		RandomListNode *tmp;
		currNode = pHead;
		while(currNode->next){
			tmp = currNode->next;
			currNode->next = tmp->next;
			currNode = tmp;
		} 
		return pCloneHead;
		 
	}
};