Interview Question: Merge two sorted singly linked lists without creating new nodes

后端 未结 26 2998
有刺的猬
有刺的猬 2020-12-02 04:09

This is a programming question asked during a written test for an interview. \"You have two singly linked lists that are already sorted, you have to merge them and return a

26条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-12-02 04:37

    A simple iterative solution.

    Node* MergeLists(Node* A, Node* B)
    {
        //handling the corner cases
    
        //if both lists are empty
        if(!A && !B)
        {
            cout << "List is empty" << endl;
            return 0;
        }
        //either of list is empty
        else if(!A) return B;
        else if(!B) return A;
        else
        {
            Node* head = NULL;//this will be the head of the newList
            Node* previous = NULL;//this will act as the
    
            /* In this algorithm we will keep the
             previous pointer that will point to the last node of the output list.
             And, as given we have A & B as pointer to the given lists.
    
             The algorithm will keep on going untill either one of the list become empty.
             Inside of the while loop, it will divide the algorithm in two parts:
                - First, if the head of the output list is not obtained yet
                - Second, if head is already there then we will just compare the values and keep appending to the 'previous' pointer.
             When one of the list become empty we will append the other 'left over' list to the output list.
             */
             while(A && B)
             {
                 if(!head)
                 {
                     if(A->data <= B->data)
                     {
                         head = A;//setting head of the output list to A
                         previous = A; //initializing previous
                         A = A->next;
                     }
                     else
                     {
                         head = B;//setting head of the output list to B
                         previous = B;//initializing previous
                         B = B->next;
                     }
                 }
                 else//when head is already set
                 {
                     if(A->data <= B->data)
                     {
                         if(previous->next != A)
                             previous->next = A;
                         A = A->next;//Moved A forward but keeping B at the same position
                     }
                     else
                     {
                         if(previous->next != B)
                             previous->next = B;
                         B = B->next; //Moved B forward but keeping A at the same position
                     }
                     previous = previous->next;//Moving the Output list pointer forward
                 }
             }
            //at the end either one of the list would finish
            //and we have to append the other list to the output list
            if(!A)
                previous->next = B;
    
            if(!B)
                previous->next = A;
    
            return head; //returning the head of the output list
        }
    }
    

提交回复
热议问题