Different approaches of merge function of Merge sort(linked list)

// 1.Least time consuming and memory efficient
// 1A.
ListNode *merge(ListNode *l1ListNode *l2)
{
    cout << "l1 ";
    printList(l1);
    cout << "l2 ";
    printList(l2);
    cout << endl
         << endl;
    // base case if (a == NULL)

    if (l1 == 0)
        return l2;
    if (l2 == 0)
        return l1;

    //take a head pointer node *c;
    ListNode *c;

    if (l1->val < l2->val)
    {
        c = l1;
        c->next = merge(l1->next, l2);
    }
    else
    {
        c = l2;
        c->next = merge(l1, l2->next);
    }
    return c;
}

//*************************************************************
//*************************************************************

// 1B.
ListNode *merge(ListNode *l1ListNode *l2)
{
    if (l1 == 0 || l2 == 0)
        return l1 != 0 ? l1 : l2;

    ListNode *dummy = new ListNode(-1);
    ListNode *head = dummy;

    ListNode *c1 = l1;
    ListNode *c2 = l2;

    while (c1 != 0 && c2 != 0)
    {
        if (c1->val <= c2->val)
        {
            head->next = c1;
            c1 = c1->next;
        }
        else
        {
            head->next = c2;
            c2 = c2->next;
        }

        head = head->next;
    }

    head->next = c1 != nullptr ? c1 : c2;

    ListNode *h = dummy->next;
    dummy->next = nullptr;
    delete dummy;
    return h;
}

//*************************************************************
//*************************************************************

// 2. Most time consuming
ListNode *merge(ListNode *l1ListNode *l2)
{
    cout << "l1 ";
    printList(l1);
    cout << "l2 ";
    printList(l2);
    ListNode *head = 0, *newnode, *temp;
    while (l1 != 0 && l2 != 0)
    {
        if (l1->val <= l2->val)
        {
            newnode = new ListNode(l1->val);
            if (head == 0)
                head = temp = newnode;
            else
            {
                temp->next = newnode;
                temp = temp->next;
            }
            l1 = l1->next;
        }
        else if (l1->val > l2->val)
        {
            newnode = new ListNode(l2->val);
            if (head == 0)
                head = temp = newnode;
            else
            {
                temp->next = newnode;
                temp = temp->next;
            }
            l2 = l2->next;
        }
    }
    while (l1 != 0)
    {
        newnode = new ListNode(l1->val);
        if (head == 0)
            head = temp = newnode;
        else
        {
            temp->next = newnode;
            temp = temp->next;
        }
        l1 = l1->next;
    }
    while (l2 != 0)
    {
        newnode = new ListNode(l2->val);
        if (head == 0)
            head = temp = newnode;
        else
        {
            temp->next = newnode;
            temp = temp->next;
        }
        l2 = l2->next;
    }
    cout << "After merging : ";
    printList(head);
    cout << endl
         << endl;
    return head;
}

Comments

Popular posts from this blog

First_Come_First_Serve CPU Scheduling

Reversing stack Method 2 !! (One Helper Stack only)

Populating Next Right Pointers in Each Node in O(1) space (without queue and level order)

Calculate factorial of large numbers !! (Using Arrays)

Multiplication of large numbers (Given in string format)

Left View of Binary Tree (Method 1 using recursion)

Check Bracket Sequence

Image Multiplication

Boundary Traversal of binary tree

BST to greater sum tree