Sorted merge of two sorted doubly circular linked lists

Given two sorted Doubly circular Linked List containing n1 and n2 nodes respectively. The problem is to merge the two lists such that resultant list is also in sorted order.

Example:
List 1:

List 2:

Final list:



Approach: Following are the steps:

  1. If head1 == NULL, return head2.
  2. If head2 == NULL, return head1.
  3. Let last1 and last2 be the last nodes of the two lists respectively. They can be obtained with the help of the previous links of the first nodes.
  4. Get pointer to the node which will be the last node of the final list. If last1.data < last2.data, then last_node = last2, Else last_node = last1.
  5. Update last1.next = last2.next = NULL.
  6. Now merge the two lists as two sorted doubly linked list are being merged. Refer merge procedure of this post. Let the first node of the final list be finalHead.
  7. Update finalHead.prev = last_node and last_node.next = finalHead.
  8. Return finalHead.
filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ implementation for Sorted merge of two
// sorted doubly circular linked list
#include <bits/stdc++.h>
using namespace std;
  
struct Node {
    int data;
    Node *next, *prev;
};
  
// A utility function to insert a new node at the
// beginning of doubly circular linked list
void insert(Node** head_ref, int data)
{
    // allocate space
    Node* new_node = new Node;
  
    // put in the data
    new_node->data = data;
  
    // if list is empty
    if (*head_ref == NULL) {
        new_node->next = new_node;
        new_node->prev = new_node;
    }
  
    else {
  
        // pointer points to last Node
        Node* last = (*head_ref)->prev;
  
        // setting up previous and next of new node
        new_node->next = *head_ref;
        new_node->prev = last;
  
        // update next and previous pointers of head_ref
        // and last.
        last->next = (*head_ref)->prev = new_node;
    }
  
    // update head_ref pointer
    *head_ref = new_node;
}
  
// function for Sorted merge of two
// sorted doubly linked list
Node* merge(Node* first, Node* second)
{
    // If first list is empty
    if (!first)
        return second;
  
    // If second list is empty
    if (!second)
        return first;
  
    // Pick the smaller value and adjust
    // the links
    if (first->data < second->data) {
        first->next = merge(first->next, second);
        first->next->prev = first;
        first->prev = NULL;
        return first;
    }
    else {
        second->next = merge(first, second->next);
        second->next->prev = second;
        second->prev = NULL;
        return second;
    }
}
  
// function for Sorted merge of two sorted
// doubly circular linked list
Node* mergeUtil(Node* head1, Node* head2)
{
    // if 1st list is empty
    if (!head1)
        return head2;
  
    // if 2nd list is empty
    if (!head2)
        return head1;
  
    // get pointer to the node which will be the
    // last node of the final list
    Node* last_node;
    if (head1->prev->data < head2->prev->data)
        last_node = head2->prev;
    else
        last_node = head1->prev;
  
    // store NULL to the 'next' link of the last nodes
    // of the two lists
    head1->prev->next = head2->prev->next = NULL;
  
    // sorted merge of head1 and head2
    Node* finalHead = merge(head1, head2);
  
    // 'prev' of 1st node pointing the last node
    // 'next' of last node pointing to 1st node
    finalHead->prev = last_node;
    last_node->next = finalHead;
  
    return finalHead;
}
  
// function to print the list
void printList(Node* head)
{
    Node* temp = head;
  
    while (temp->next != head) {
        cout << temp->data << " ";
        temp = temp->next;
    }
    cout << temp->data << " ";
}
  
// Driver program to test above
int main()
{
    Node *head1 = NULL, *head2 = NULL;
  
    // list 1:
    insert(&head1, 8);
    insert(&head1, 5);
    insert(&head1, 3);
    insert(&head1, 1);
  
    // list 2:
    insert(&head2, 11);
    insert(&head2, 9);
    insert(&head2, 7);
    insert(&head2, 2);
  
    Node* newHead = mergeUtil(head1, head2);
  
    cout << "Final Sorted List: ";
    printList(newHead);
  
    return 0;
}

chevron_right


Output:

Final Sorted List: 1 2 3 5 7 8 9 11

Time Complexity: O(n1 + n2).



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.