Mohit Kumar Mohit Kumar - 13 days ago 6
C++ Question

Not able to merge Sorted Linked Lists using Auxiliary Linked List

Please give a simple solution to problem. I have used the algorithm like mergesort but I am not able return head of the auxiliary Linked list, I created. I have seen other example on stack overflow. But I want to know where is the problem with my code.

/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
ListNode* Solution::mergeTwoLists(ListNode* A, ListNode* B) {

ListNode* head;
ListNode* root;

ListNode* H1 = A;
ListNode* H2 = B;
int flag = 0;
while (H1 != NULL && H2 != NULL){
if(H1->val < H2->val){
root = new ListNode(H1->val);
//cout << root->val << " ";
if (flag = 0){
head = root;
flag = 1;
}

//root->next = el;
root = root->next;
H1 = H1->next;
}else{
root = new ListNode(H2->val);
if (flag = 0){
head =root;
flag = 1;
}
//cout << root->val << " ";
//root->next = el;
root = root->next;
H2 = H2->next;
}
}
while (H2 != NULL){

root = new ListNode(H2->val);
//cout << root->val << " ";
//root->next = el;
root = root->next;
H2 = H2->next;
}
while (H1 != NULL){
root = new ListNode(H1->val);
//cout << root->val << " ";
//root->next = el;
root = root->next;
H1 = H1->next;
}

ListNode *start=head;
while(start)
{
cout<<start->val<<" ";
start=start->next;
}


return head;
}


I have used cout to know the order, It gives the correct order. I am missing something here. None of the list are NULL

Answer

Two issues found in your code. At first the equal operator should be changed to Boolean in two places:

if (flag = 0){

should be

if (flag == 0){

Then, a tail node should be kept while traversing the two lists.

I converted the code in this (applying minimum changes) which works:

ListNode* mergeTwoLists(ListNode* A, ListNode* B) {

    ListNode* head;
    ListNode* tail; //<-- a tail is introduced
    ListNode* root;

    ListNode* H1 = A;
    ListNode* H2 = B;
    int flag = 0;
    while (H1 != NULL && H2 != NULL){
        if(H1->val < H2->val){
            root = new ListNode(H1->val);
            //cout << root->val << " ";
            if (flag == 0){ //<-- fixed
                 head = root;
                 tail=head;
                flag = 1;
            }
            else
            {
                tail->next=root;
                tail = root;
            }


            //root->next = el;
            //root = root->next;
            H1 = H1->next;
        }else{
            root = new ListNode(H2->val);
            if (flag == 0){  //<-- fixed
               head  =root;
               tail=head;
                flag = 1;
            }
            else
            {
                tail->next=root;
                tail = root;
            }
            //cout << root->val << " ";
            //root->next = el;
           // root = root->next;
            H2 = H2->next;
        }
    }
    while (H2 != NULL){

        root = new ListNode(H2->val);
        //cout << root->val << " ";
        //root->next = el;
        tail->next=root;
        tail=root;
       // root = root->next;
        H2 = H2->next;
    }
    while (H1 != NULL){
        root = new ListNode(H1->val);
        //cout << root->val << " ";
        //root->next = el;
        tail->next=root;
        tail=root;
        //root = root->next;
        H1 = H1->next;
    }

     ListNode *start=head;
        while(start)
          {
            cout<<start->val<<" ";
            start=start->next;
          }


    return head;
}
Comments