Clone a linked list with next and random pointer
PROBLEM :
You are given a Double Link List with one pointer of each node pointing to the next node just like in a single link list. The second pointer which is arbitary pointer which can point to any node in the list and not just the previous node.
The task is to complete the function copyList which take one argument the head of the linked list to be copied and should return the head of the copied linked list.
Input:
You have to complete the method which takes one argument: the head of the linked list. You should not read any input from stdin/console.
The struct Node has a data part which stores the data and a next pointer which points to the next element of the linked list and an arbitrary pointer (arb) which points to any arbitrary node .
There are multiple test cases. For each test case, this method will be called individually.
Output:
Your function should return the head of the copied list.
Constraints:
1 <=T<= 100
1 <=N<= 100
1 <=Q<= 100
1 <=a,b<= 100
Example:
Input:
1
4 2
1 2 3 4
1 2 2 4
The first line above represents T, i.e., the number of test cases. The second line is of the form N Q, where N is the number of nodes and Q is the number of nodes with arbitrary pointers. Third line presents all the nodes of the list. The fourth line represents Q pairs of integers in the order (a b) where a is the pointer and b is the node being pointed to (See explanation below for more understanding).
Explanation of the example: There is one test case. In this test case, there are 4 nodes in linked list. Among these 4 nodes, 2 nodes have arbit pointer set, rest two nodes have arbit pointer as NULL. Third line tells us the value of four nodes. The fourth line gives the information about arbitrary pointers. The first node with set arbit pointer is 1, its arbit pointer is pointing to node 2. The second node with set arbit pointer is 2, its arbit pointer is pointing to node 4.
Output:
1
The output is 1 if linked list is successfully cloned, else false.
Note : The Input/Output format and Example given are used for system's internal purpose, and should be used by a user for Expected Output only. As it is a function problem, hence a user should not read any input from stdin/console, and should not print anything on stdout/console. The task is to complete the function specified, and not to write the full code.
--------------------------------------------------------------------------------
SIMPLE c++ IMPLEMENTATION :
--------------------------------------------------------------------------------
/* the node structure is as follows
struct Node
{
int data;
Node* next;
Node* arb;
};*/
// Should return the head of the copied linked list the
//output will be 1 if successfully copied
Node *rearrangeEvenOdd(Node *) ;
Node * copyList(Node *head)
{
if(head==NULL||head->next==NULL)
return head ;
Node *temp,*curr ;
curr=head ;
while(curr){
temp=curr->next ;
Node *ptr=(Node*)malloc(sizeof(Node)) ;
ptr->data=curr->data ;
ptr->next=curr->next ;
curr->next=ptr ;
curr=temp ;
}
curr=head ;
temp=curr->next ;
while(true){
if(curr->arb!=NULL)
temp->arb=curr->arb->next ;
else
temp->arb=NULL ;
curr=curr->next->next ;
if(curr==NULL)
break ;
temp=temp->next->next ;
}
return rearrangeEvenOdd(head) ;
}
Node *rearrangeEvenOdd(Node *head)
{
if(head==NULL||head->next==NULL)
return head ;
Node *first,*second ;
first=head ;
second=head->next ;
Node *temp,*ptr ;
ptr=first ;
while(ptr->next->next!=NULL){
temp=ptr->next ;
ptr->next=ptr->next->next ;
ptr=temp ;
}
temp=ptr->next ;
temp->next=NULL ;
ptr->next=NULL ;
return second ;
}
---------------------------------------------------------------------------------
You are given a Double Link List with one pointer of each node pointing to the next node just like in a single link list. The second pointer which is arbitary pointer which can point to any node in the list and not just the previous node.
The task is to complete the function copyList which take one argument the head of the linked list to be copied and should return the head of the copied linked list.
Input:
You have to complete the method which takes one argument: the head of the linked list. You should not read any input from stdin/console.
The struct Node has a data part which stores the data and a next pointer which points to the next element of the linked list and an arbitrary pointer (arb) which points to any arbitrary node .
There are multiple test cases. For each test case, this method will be called individually.
Output:
Your function should return the head of the copied list.
Constraints:
1 <=T<= 100
1 <=N<= 100
1 <=Q<= 100
1 <=a,b<= 100
Example:
Input:
1
4 2
1 2 3 4
1 2 2 4
The first line above represents T, i.e., the number of test cases. The second line is of the form N Q, where N is the number of nodes and Q is the number of nodes with arbitrary pointers. Third line presents all the nodes of the list. The fourth line represents Q pairs of integers in the order (a b) where a is the pointer and b is the node being pointed to (See explanation below for more understanding).
Explanation of the example: There is one test case. In this test case, there are 4 nodes in linked list. Among these 4 nodes, 2 nodes have arbit pointer set, rest two nodes have arbit pointer as NULL. Third line tells us the value of four nodes. The fourth line gives the information about arbitrary pointers. The first node with set arbit pointer is 1, its arbit pointer is pointing to node 2. The second node with set arbit pointer is 2, its arbit pointer is pointing to node 4.
Output:
1
The output is 1 if linked list is successfully cloned, else false.
Note : The Input/Output format and Example given are used for system's internal purpose, and should be used by a user for Expected Output only. As it is a function problem, hence a user should not read any input from stdin/console, and should not print anything on stdout/console. The task is to complete the function specified, and not to write the full code.
--------------------------------------------------------------------------------
SIMPLE c++ IMPLEMENTATION :
--------------------------------------------------------------------------------
/* the node structure is as follows
struct Node
{
int data;
Node* next;
Node* arb;
};*/
// Should return the head of the copied linked list the
//output will be 1 if successfully copied
Node *rearrangeEvenOdd(Node *) ;
Node * copyList(Node *head)
{
if(head==NULL||head->next==NULL)
return head ;
Node *temp,*curr ;
curr=head ;
while(curr){
temp=curr->next ;
Node *ptr=(Node*)malloc(sizeof(Node)) ;
ptr->data=curr->data ;
ptr->next=curr->next ;
curr->next=ptr ;
curr=temp ;
}
curr=head ;
temp=curr->next ;
while(true){
if(curr->arb!=NULL)
temp->arb=curr->arb->next ;
else
temp->arb=NULL ;
curr=curr->next->next ;
if(curr==NULL)
break ;
temp=temp->next->next ;
}
return rearrangeEvenOdd(head) ;
}
Node *rearrangeEvenOdd(Node *head)
{
if(head==NULL||head->next==NULL)
return head ;
Node *first,*second ;
first=head ;
second=head->next ;
Node *temp,*ptr ;
ptr=first ;
while(ptr->next->next!=NULL){
temp=ptr->next ;
ptr->next=ptr->next->next ;
ptr=temp ;
}
temp=ptr->next ;
temp->next=NULL ;
ptr->next=NULL ;
return second ;
}
---------------------------------------------------------------------------------
Comments
Post a Comment