i m submitting below ans fr this ques but error is coming
Practicing ll questions
Given singly linked list with every node having an additional “arbitrary” pointer. This arbitrary pointer is currently pointing to NULL. You need to make this arbitrary pointer of each node point to the next higher value node in the input list.
initially arbitrary pointer is NULL
so in your code
if(head == NULL || head->arbit == NULL){
return head;
}
this will be always correct
i hope you understand your mistake
now alsoit is giving error
/*************
Following is the Node structure already written.
template
class Node
{
public:
T data;
Node* next, *arbit;
Node(T data)
{
this->data=data;
next=NULL;
arbit=NULL;
}
};
************/
Node merge(Node *a,Node *b){
if(a==NULL){
return b;
}
else if(b==NULL){
return a;
}
Nodec;
if(a->datadata){
c = a;
c->arbit = merge(a->arbit,b);
}
else{
c = b;
c->arbit = merge(a,b->arbit);
}
return c;
}
Node mergeSort(Node *head){
if(head == NULL || head->arbit == NULL){
return head;
}
//1.mid pt
Node<int>*slow = head;
Node<int>*fast = head;
while(fast!=NULL && fast->arbit!=NULL){
fast = fast->arbit->arbit;
slow = slow->arbit;
}
Node<int>*mid = slow;
Node<int>*a = head;
Node<int>*b = mid->arbit;
mid->next = NULL;
a = mergeSort(a);
b = mergeSort(b);
Node<int>*c = merge(a,b);
return c;
}
Node* arbit(Node *head){
// Write your code here
Node*temp = head;
while(temp!=NULL){
temp->arbit = temp->next;
temp = temp->next;
}
Node*head_new = mergeSort(head);
return head_new;
}
please send link of code
approach is correct
mistake is in merge sort function
it have done some modification
at line no 44
Modified Code
at line no 60
cout<<endl;
if you don’t use it then it will give wrong answer on all testcase
this is mistake of problem setter i got it from there sample output
to figure out this i waste more than 15 min
i hope this helps
if yes hit a like and don’t forgot to mark doubt as resolved
if you have more doubts regarding this feel free to ask
it is still giving runtime error
/*************
Following is the Node structure already written.
template
class Node
{
public:
T data;
Node* next, *arbit;
Node(T data)
{
this->data=data;
next=NULL;
arbit=NULL;
}
};
************/
Node merge(Node *a,Node *b){
if(a==NULL){
return b;
}
else if(b==NULL){
return a;
}
Nodec;
if(a->datadata){
c = a;
c->arbit = merge(a->arbit,b);
}
else{
c = b;
c->arbit = merge(a,b->arbit);
}
return c;
}
Node mergeSort(Node *head){
if(head == NULL || head->arbit == NULL){
return head;
}
//1.mid pt
Node<int>*slow = head;
Node<int>*fast = head->temp;
while(fast!=NULL && fast->arbit!=NULL){
fast = fast->arbit->arbit;
slow = slow->arbit;
}
Node<int>*mid = slow;
Node<int>*a = head;
Node<int>*b = mid->arbit;
mid->next = NULL;
a = mergeSort(a);
b = mergeSort(b);
Node<int>*c = merge(a,b);
return c;
}
Node* arbit(Node *head){
// Write your code here
cout<<endl;
Node*temp = head;
while(temp!=NULL){
temp->arbit = temp->next;
temp = temp->next;
}
Node*head_new = mergeSort(head);
return head_new;
}
this should be mid->arbit=NULL;
see at Modified code line no 52
i think i have solved your doubt
so please mark your doubt as resolved and give your valueable feedback
I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.
On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.