Search code examples
c++algorithmpointerslinked-listmergesort

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


Solution

  • /**
     * 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) {
          if (A == NULL){
          return B;
          }
          if (B == NULL){
              return A;
          }
    
        ListNode* head;
        ListNode* root = new ListNode(0); //initialized root
        ListNode* H1 = A;
        ListNode* H2 = B;
        if(H1->val < H2->val){
                root->val = H1->val;
                head = root;
                H1 = H1->next;
            }else{
                root->val = H2->val;
                head = root;
                H2 = H2->next;
            }
    
    
        while (H1 != NULL && H2 != NULL){
            if(H1->val < H2->val){
                root->next = new ListNode(H1->val);
                root = root->next;            //making list
                H1 = H1->next;
            }else{
                root->next = new ListNode(H2->val);
                root = root->next;
                H2 = H2->next;
            }
        }
        while (H2 != NULL){
            root->next = new ListNode(H2->val);
            root = root->next;
            H2 = H2->next;
        }
        while (H1 != NULL){
            root->next = new ListNode(H1->val);
            root = root->next;
            H1 = H1->next;
        }
    
    
    
        return head;
    }
    

    Initialization wasn't done properly