0
What is problem with my code ?
This is code of merge sort in linked list . I don't know why it is giving error. Please help me . https://code.sololearn.com/cVn7i6a994gK/?ref=app
2 Respostas
+ 3
Your code is failing when you try to create a Node instance with new Node(-1)
Where is your constructor that defines what to do with the integer argument? Default (implicit) constructor has no arguments.
+ 2
other reason than the one @Tibor Santa pointed out, in the "findMid" function:
the fast pointer should point to the second node of the list.
Node* fast = head->next;
so if there’s only one node in the list, the fast pointer will point to the null pointer, at each step, we move the slow pointer by one step and the fast pointer by two steps. in doing so, when the fast pointer either reaches the last node of the linked list or it points to a null pointer, the slow pointer would have reached the middle of the list.
more precisely, in an odd-length linked list, the fast pointer reaches the null pointer whereas, in an even-length linked list, the fast pointer reaches the last node at the same iteration when the slow pointer reaches the middle node.
Node * findMid(Node * head)
{
// since the fast pointer points to the second node, we need to check the head
if ( head == NULL )
return head;
Node * slow = head ;
Node * fast = head->next;
...
...
...
}
https://www.sololearn.com/compiler-playground/cN8pkFCnY59C