Foundations of Fortune left class3- title list containing 13 random replication node pointer
1. Title: Replication random list containing node pointer
[Title] A special class list node is described as follows:
public class the Node
{
public int value;
public Next the Node;
public the Node RAND;
public the Node (int Data) = Data {this.value;}
}
the Node class value is node value, and the next pointer, as in the normal sense single list next pointer pointing to the next node, the pointer RAND is new node class pointer that may point to any node in the linked list, may point to null. Given a single head node chain acyclic head Node by node type of composition, implement a function to copy all the complete configuration of the linked list, and returns a list of the new head node replication.
Advanced: no additional data structures, only a limited number of variables, and completion of the time complexity is a function of the original problem to be achieved in (N) O.
2. Analysis
Title data structure given above, there rand list, next two pointers, next pointer is a pointer rand 1-> 2-> 3.1 point to point rand 3,2- 1,3 pointing null, the entire copy requires list.
A method of: using a hash table (not cooked hash table, later supplemented)
Method two: the original and then modify the list based on the
idea: See 1,2,3 three nodes as shown in FIG alone, considered in each forming the following nodes added to three new nodes connected to nodes 2, 3 a large linked list, and then were replicated three nodes, and finally isolated a new copy of the subject to achieve the requirements list
3. core code
(1) generating a title list provided
typedef struct ListNode
{
int val;
ListNode *next;
ListNode *rand;
}*List;
① generating head node
List MakeEmpty()
{
List head = (List)malloc(sizeof(ListNode));
head->next = NULL;
return head;
}
② tail interpolation data insertion
void insert_next(List head,int num)
{
//p是新加入的节点
List p = (List)malloc(sizeof(ListNode));
p->val = num;
p->next = NULL;
p->rand = NULL;
List r = head;
while(r->next != NULL)//r指向末尾
{
r = r->next;
}
r->next = p;//p加入链表
}
③ added rand pointer
added 1-> 3,2-> 1,3-> NULL directly in the main function
List cur = head->next;
cur->rand = cur->next->next;
cur->next->rand = cur;
cur->next->next->rand = NULL;
(2) adding a new node
Because the addition of new nodes, the original list will be interrupted, so that the position of the next node nex be recorded in advance, the present step corresponding to the insertion node at the current location.
//加入链
head = head->next;
List cur = head;//cur当前节点
List nex = NULL;
while(cur != NULL)
{
nex = cur->next;
List p = (List)malloc(sizeof(ListNode));
cur->next = p;
p->next = nex;
cur = nex;
}
(3) Copy the value rand and the pointer to the new node
For example, 1-> 3, then 1 * -> * 3, using two pointers point to the current node and the new node, 1 *. 1 should point 1-> rand (ie 3) -> next (ie 3 ), the equivalent of a new node to the next rand rand old node. Of course, rand 3 points to NULL, there is no next pointer, and otherwise determines the need to add error.
//copy
cur = head;
List cur_copy = NULL;//新节点
while(cur != NULL )
{
cur_copy = cur->next;
cur_copy->rand = cur->rand == NULL ? NULL : cur->rand->next;//拷贝指针
cur_copy->val = cur->val;//拷贝值
cur = cur_copy->next;
}
(4) copies of the partially separated
To link directly to the new node, and also through the two pointers cur cur_copy
//分离拷贝的部分
List res = head->next;//返回res新节点位置
cur = head;
while(cur != NULL)
{
nex = cur->next->next;//记录下一个旧节点
cur_copy = cur->next;
cur_copy->next = nex == NULL ? NULL : cur_copy->next->next;
cur = nex;//cur相当于只在旧节点1,2,3之间寻找
}
4. The complete code
#include<iostream>
using namespace std;
typedef struct ListNode
{
int val;
ListNode *next;
ListNode *rand;
}*List;
List copy_list(List head)
{
//加入链
head = head->next;
List cur = head;//当前节点
List nex = NULL;
while(cur != NULL)
{
nex = cur->next;
List p = (List)malloc(sizeof(ListNode));
cur->next = p;
p->next = nex;
cur = nex;
}
//copy
cur = head;
List cur_copy = NULL;
while(cur != NULL )
{
cur_copy = cur->next;
cur_copy->rand = cur->rand == NULL ? NULL : cur->rand->next;
cur_copy->val = cur->val;
cur = cur_copy->next;
}
//分离拷贝的部分
List res = head->next;
cur = head;
while(cur != NULL)
{
nex = cur->next->next;//记录下一个旧节点
cur_copy = cur->next;
cur_copy->next = nex == NULL ? NULL : cur_copy->next->next;
cur = nex;//cur相当于只在旧节点1,2,3之间寻找
}
return res;
}
void insert_next(List head,int num)
{
List p = (List)malloc(sizeof(ListNode));
p->val = num;
p->next = NULL;
p->rand = NULL;
List r = head;
while(r->next != NULL)
{
r = r->next;
}
r->next = p;
}
List MakeEmpty()
{
List head = (List)malloc(sizeof(ListNode));
head->next = NULL;
return head;
}
int main()
{
List head = MakeEmpty();
insert_next(head,5);
insert_next(head,7);
insert_next(head,9);
List cur = head->next;
cur->rand = cur->next->next;
cur->next->rand = cur;
cur->next->next->rand = NULL;
List res = copy_list(head);
system("pause");
return 0;
}