Given two sorted linked lists consisting of N and M nodes respectively. The task is to merge both of the list (in-place) and return head of the merged list.
Note: It is strongly recommended to do merging in-place using O(1) extra space.
QUESTION URL:https://practice.geeksforgeeks.org/problems/merge-two-sorted-linked-lists/1
My solution in C++:(This is only the main code )
struct Node {
int data;
struct Node *next;
Node(int x) {
data = x;
next = NULL;
}
};
Node* sortedMerge(Node* head_A, Node* head_B)
{
Node* third = NULL;
Node* last = NULL;
if(head_A->data < head_B->data)
{
third = head_A;
last = head_A;
head_A = head_A->next;
third->next = NULL;
}
else
{
third = head_B;
last = head_B;
head_B = head_B->next;
third->next = NULL;
}
while(head_A && head_B)
{
if(head_A->data < head_B->data)
{
last->next = head_A;
last = head_A;
head_A = head_A->next;
last->next = NULL;
}
if(head_A->data > head_B->data)
{
last->next = head_B;
last = head_B;
head_B = head_B->next;
last->next = NULL;
}
}
if(head_A)
{
last->next = head_A;
}
if(head_B)
{
last->next = head_B;
}
return third;
}
I am getting segmentation error when i was submitted and i want know the test case where i am getting segmentation error