TopicWise/Linked List/MergeSortLinkedList.cpp
/* Function to mergesort the linked list
   It should return head of the modified list  */
Function to mergesort the linked list
It should return head of the modified list
Function to mergesort the linked list It should return head of the modified list
Node *sortedMerge(Node *a, Node *b)
{
    if(a == NULL)
    return b;
    if(b == NULL)
    return a;
    Node *result = new Node(0);
    Node *dummy;
    dummy = result;
    Node *tempA = a;
    Node *tempB = b;
    while(tempA != NULL && tempB != NULL)
    {
        if(tempA->data > tempB->data)
        {
            dummy->next = tempB;
            tempB = tempB->next;
        }
        else
        {
            dummy->next = tempA;
            tempA = tempA -> next;
        }
        dummy = dummy->next;
    }
    if(tempA != NULL)
    dummy->next = tempA;
    if(tempB != NULL)
    dummy->next = tempB;
    return result->next;
}
Node * sortedMerge ( Node * a , Node * b ) { if ( a == NULL ) return b ; if ( b == NULL ) return a ; Node * result = new Node ( 0 ) ; Node * dummy ; dummy = result ; Node * tempA = a ; Node * tempB = b ; while ( tempA != NULL && tempB != NULL ) { if ( tempA -> data > tempB -> data ) { dummy -> next = tempB ; tempB = tempB -> next ; } else { dummy -> next = tempA ; tempA = tempA -> next ; } dummy = dummy -> next ; } if ( tempA != NULL ) dummy -> next = tempA ; if ( tempB != NULL ) dummy -> next = tempB ; return result -> next ; }
Function to mergesort the linked list
It should return head of the modified list
Function to mergesort the linked list It should return head of the modified list