Open In App

Sorted merge of two sorted doubly circular linked lists

Last Updated : 04 May, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

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

Example: 

Input:
List 1: 

List 2: 

Output:

[Naive Approach] Using Array and Sorting - O((n+m) * log(n+m)) time and O(n+m) space

The idea is to store the elements of both the lists in an array and then apply sorting to it. After sorting, create a new list, traverse the array and append a node with the corresponding value into the list. After the traversal, link the first and last nodes of the list to make it circular.

C++
// C++ program to merge two sorted doubly
// circular linked lists using array
#include <bits/stdc++.h>
using namespace std;

class Node {
public:
    int data;
    Node *next;
    Node *prev;
    
    Node(int x) {
        data = x;
        next = nullptr;
        prev = nullptr;
    }
};

// Function to merge two sorted doubly circular linked lists
Node *sortedMerge(Node *head1, Node *head2) {
    vector<int> arr;
    
    // If either list is empty, return the other list
    if (head1 == nullptr) return head2;
    if (head2 == nullptr) return head1;
    
    // Pushing values of the first circular linked list
    Node *curr1 = head1;
    do {
        arr.push_back(curr1->data);
        curr1 = curr1->next;
    } while (curr1 != head1);
    
    // Pushing values of the second circular linked list
    Node *curr2 = head2;
    do {
        arr.push_back(curr2->data);
        curr2 = curr2->next;
    } while (curr2 != head2);
    
    // Sorting the array 
    sort(arr.begin(), arr.end());
    
    // Creating a new doubly circular 
    // linked list with sorted values
    Node *dummy = new Node(-1);
    Node *curr = dummy;
    
    for (int i = 0; i < arr.size(); i++) {
        curr->next = new Node(arr[i]);
        curr->next->prev = curr;
        curr = curr->next;
    }
    
    // Making the list circular
    if (dummy->next != nullptr) {
        Node *first = dummy->next;
        
        // Last node points to first node
        curr->next = first;  
        
        // First node's prev points to last node
        first->prev = curr;      
    }
    
    return dummy->next;
}

int main() {
    // First dll: 5 -> 10 -> 15
    Node *head1 = new Node(5);
    Node *second1 = new Node(10);
    Node *third1 = new Node(15);
    
    head1->next = second1;
    second1->prev = head1;
    second1->next = third1;
    third1->prev = second1;
    third1->next = head1;    
    head1->prev = third1;
    
    // Second dll: 2 -> 3 -> 20
    Node *head2 = new Node(2);
    Node *second2 = new Node(3);
    Node *third2 = new Node(20);
    
    head2->next = second2;
    second2->prev = head2;
    second2->next = third2;
    third2->prev = second2;
    third2->next = head2; 
    head2->prev = third2;
    
    Node *res = sortedMerge(head1, head2);
    Node* curr = res;
    do {
        cout << curr->data << " ";
        curr = curr->next;
    } while (curr != res);
    
    return 0;
}
Java
// Java program to merge two sorted doubly
// circular linked lists using array
import java.util.*;

class Node {
    int data;
    Node next;
    Node prev;

    Node(int x) {
        data = x;
        next = null;
        prev = null;
    }
}

class GfG {

    // Function to merge two sorted doubly circular linked lists
    static Node sortedMerge(Node head1, Node head2) {
        ArrayList<Integer> arr = new ArrayList<>();

        // If either list is empty, return the other list
        if (head1 == null) return head2;
        if (head2 == null) return head1;

        // Pushing values of the first circular linked list
        Node curr1 = head1;
        do {
            arr.add(curr1.data);
            curr1 = curr1.next;
        } while (curr1 != head1);

        // Pushing values of the second circular linked list
        Node curr2 = head2;
        do {
            arr.add(curr2.data);
            curr2 = curr2.next;
        } while (curr2 != head2);

        // Sorting the array 
        Collections.sort(arr);

        // Creating a new doubly circular 
        // linked list with sorted values
        Node dummy = new Node(-1);
        Node curr = dummy;

        for (int i = 0; i < arr.size(); i++) {
            curr.next = new Node(arr.get(i));
            curr.next.prev = curr;
            curr = curr.next;
        }

        // Making the list circular
        if (dummy.next != null) {
            Node first = dummy.next;

            // Last node points to first node
            curr.next = first;  

            // First node's prev points to last node
            first.prev = curr;      
        }

        return dummy.next;
    }

    public static void main(String[] args) {
        
        // First dll: 5 -> 10 -> 15
        Node head1 = new Node(5);
        Node second1 = new Node(10);
        Node third1 = new Node(15);

        head1.next = second1;
        second1.prev = head1;
        second1.next = third1;
        third1.prev = second1;
        third1.next = head1;    
        head1.prev = third1;

        // Second dll: 2 -> 3 -> 20
        Node head2 = new Node(2);
        Node second2 = new Node(3);
        Node third2 = new Node(20);

        head2.next = second2;
        second2.prev = head2;
        second2.next = third2;
        third2.prev = second2;
        third2.next = head2; 
        head2.prev = third2;

        Node res = sortedMerge(head1, head2);
        Node curr = res;
        do {
            System.out.print(curr.data + " ");
            curr = curr.next;
        } while (curr != res);
    }
}
Python
# Python program to merge two sorted doubly
# circular linked lists using array

# Node class
class Node:
    def __init__(self, x):
        self.data = x
        self.next = None
        self.prev = None

# Function to merge two sorted doubly circular linked lists
def sortedMerge(head1, head2):
    arr = []

    # If either list is empty, return the other list
    if head1 is None:
        return head2
    if head2 is None:
        return head1

    # Pushing values of the first circular linked list
    curr1 = head1
    while True:
        arr.append(curr1.data)
        curr1 = curr1.next
        if curr1 == head1:
            break

    # Pushing values of the second circular linked list
    curr2 = head2
    while True:
        arr.append(curr2.data)
        curr2 = curr2.next
        if curr2 == head2:
            break

    # Sorting the array 
    arr.sort()

    # Creating a new doubly circular 
    # linked list with sorted values
    dummy = Node(-1)
    curr = dummy

    for val in arr:
        curr.next = Node(val)
        curr.next.prev = curr
        curr = curr.next

    # Making the list circular
    if dummy.next is not None:
        first = dummy.next

        # Last node points to first node
        curr.next = first

        # First node's prev points to last node
        first.prev = curr

    return dummy.next

if __name__ == "__main__":
    # First dll: 5 -> 10 -> 15
    head1 = Node(5)
    second1 = Node(10)
    third1 = Node(15)

    head1.next = second1
    second1.prev = head1
    second1.next = third1
    third1.prev = second1
    third1.next = head1    
    head1.prev = third1

    # Second dll: 2 -> 3 -> 20
    head2 = Node(2)
    second2 = Node(3)
    third2 = Node(20)

    head2.next = second2
    second2.prev = head2
    second2.next = third2
    third2.prev = second2
    third2.next = head2 
    head2.prev = third2

    res = sortedMerge(head1, head2)
    curr = res
    while True:
        print(curr.data, end=" ")
        curr = curr.next
        if curr == res:
            break
C#
// C# program to merge two sorted doubly
// circular linked lists using array
using System;
using System.Collections.Generic;

class Node {
    public int data;
    public Node next;
    public Node prev;

    public Node(int x) {
        data = x;
        next = null;
        prev = null;
    }
}

class GfG {

    // Function to merge two sorted doubly circular linked lists
    static Node sortedMerge(Node head1, Node head2) {
        List<int> arr = new List<int>();

        // If either list is empty, return the other list
        if (head1 == null) return head2;
        if (head2 == null) return head1;

        // Pushing values of the first circular linked list
        Node curr1 = head1;
        do {
            arr.Add(curr1.data);
            curr1 = curr1.next;
        } while (curr1 != head1);

        // Pushing values of the second circular linked list
        Node curr2 = head2;
        do {
            arr.Add(curr2.data);
            curr2 = curr2.next;
        } while (curr2 != head2);

        // Sorting the array 
        arr.Sort();

        // Creating a new doubly circular 
        // linked list with sorted values
        Node dummy = new Node(-1);
        Node curr = dummy;

        for (int i = 0; i < arr.Count; i++) {
            curr.next = new Node(arr[i]);
            curr.next.prev = curr;
            curr = curr.next;
        }

        // Making the list circular
        if (dummy.next != null) {
            Node first = dummy.next;

            // Last node points to first node
            curr.next = first;  

            // First node's prev points to last node
            first.prev = curr;      
        }

        return dummy.next;
    }

    static void Main() {
        
        // First dll: 5 -> 10 -> 15
        Node head1 = new Node(5);
        Node second1 = new Node(10);
        Node third1 = new Node(15);

        head1.next = second1;
        second1.prev = head1;
        second1.next = third1;
        third1.prev = second1;
        third1.next = head1;    
        head1.prev = third1;

        // Second dll: 2 -> 3 -> 20
        Node head2 = new Node(2);
        Node second2 = new Node(3);
        Node third2 = new Node(20);

        head2.next = second2;
        second2.prev = head2;
        second2.next = third2;
        third2.prev = second2;
        third2.next = head2; 
        head2.prev = third2;

        Node res = sortedMerge(head1, head2);
        Node curr = res;
        do {
            Console.Write(curr.data + " ");
            curr = curr.next;
        } while (curr != res);
    }
}
JavaScript
// JavaScript program to merge two sorted doubly
// circular linked lists using array

class Node {
    constructor(x) {
        this.data = x;
        this.next = null;
        this.prev = null;
    }
}

// Function to merge two sorted doubly circular linked lists
function sortedMerge(head1, head2) {
    let arr = [];

    // If either list is empty, return the other list
    if (head1 === null) return head2;
    if (head2 === null) return head1;

    // Pushing values of the first circular linked list
    let curr1 = head1;
    do {
        arr.push(curr1.data);
        curr1 = curr1.next;
    } while (curr1 !== head1);

    // Pushing values of the second circular linked list
    let curr2 = head2;
    do {
        arr.push(curr2.data);
        curr2 = curr2.next;
    } while (curr2 !== head2);

    // Sorting the array 
    arr.sort((a, b) => a - b);

    // Creating a new doubly circular 
    // linked list with sorted values
    let dummy = new Node(-1);
    let curr = dummy;

    for (let i = 0; i < arr.length; i++) {
        curr.next = new Node(arr[i]);
        curr.next.prev = curr;
        curr = curr.next;
    }

    // Making the list circular
    if (dummy.next !== null) {
        let first = dummy.next;

        // Last node points to first node
        curr.next = first;

        // First node's prev points to last node
        first.prev = curr;
    }

    return dummy.next;
}

// First dll: 5 -> 10 -> 15
let head1 = new Node(5);
let second1 = new Node(10);
let third1 = new Node(15);

head1.next = second1;
second1.prev = head1;
second1.next = third1;
third1.prev = second1;
third1.next = head1;
head1.prev = third1;

// Second dll: 2 -> 3 -> 20
let head2 = new Node(2);
let second2 = new Node(3);
let third2 = new Node(20);

head2.next = second2;
second2.prev = head2;
second2.next = third2;
third2.prev = second2;
third2.next = head2;
head2.prev = third2;

let res = sortedMerge(head1, head2);
let curr = res;
do {
    console.log(curr.data);
    curr = curr.next;
} while (curr !== res);

Output
2 3 5 10 15 20 

[Expected Approach] In-Place Merge - O(n + m) time and O(n + m) space

The idea is to merge two sorted doubly circular linked lists by iteratively comparing nodes from both lists, selecting the smaller one to add to the result, and finally restoring the circular property by connecting the last node to the first node of the merged list. We use a dummy node to avoid extra if else conditions.

Step by step approach:

  1. Break the circular property of both lists by disconnecting the last and first nodes.
  2. Create a dummy node and use it as a starting point for the merged list.
  3. Compare heads of both lists and attach the smaller one to the result, updating previous pointers.
  4. Advance pointers for both the result and the list from which a node was taken.
  5. Make the merged list circular by connecting the last node to the first node.
C++
// C++ program to merge two sorted doubly
// circular linked lists iteratively
#include <bits/stdc++.h>
using namespace std;

class Node {
public:
    int data;
    Node* next;
    Node* prev;
    
    Node(int x) {
        data = x;
        next = nullptr;
        prev = nullptr;
    }
};

// Function to merge two sorted doubly 
// circular linked lists iteratively
Node* sortedMerge(Node* head1, Node* head2) {
    
    // If either list is empty, return the other list
    if (head1 == nullptr) return head2;
    if (head2 == nullptr) return head1;
    
    // Find the last nodes of both lists
    Node* last1 = head1->prev;
    Node* last2 = head2->prev;
    
    // Create a dummy node to simplify the merging process
    Node* dummy = new Node(-1);
    Node* curr = dummy;
    
    // Break the circular property temporarily
    last1->next = nullptr;
    last2->next = nullptr;
    
    // Iterate through both linked lists
    while (head1 != nullptr && head2 != nullptr) {
        
        // Add the smaller node to the merged list
        if (head1->data <= head2->data) {
            curr->next = head1;
            head1->prev = curr;
            head1 = head1->next;
        } else {
            curr->next = head2;
            head2->prev = curr;
            head2 = head2->next;
        }
        curr = curr->next;
    }
    
    // If any list is left, append it to the merged list
    if (head1 != nullptr) {
        curr->next = head1;
        head1->prev = curr;
    } else if (head2 != nullptr) {
        curr->next = head2;
        head2->prev = curr;
    }
    
    // Find the last node of the merged list
    Node* result = dummy->next;
    Node* lastNode = result;
    while (lastNode->next != nullptr) {
        lastNode = lastNode->next;
    }
    
    // Make the merged list circular
    lastNode->next = result;
    result->prev = lastNode;
    
    return result;
}

void printList(Node* head) {
    if (head == nullptr) return;
    
    Node* curr = head;
    do {
        cout << curr->data;
        curr = curr->next;
        if (curr != head) cout << " ";
    } while (curr != head);
    cout << endl;
}

int main() {
    // First circular dll: 5 -> 10 -> 15 -> 40
    Node* head1 = new Node(5);
    Node* second1 = new Node(10);
    Node* third1 = new Node(15);
    Node* fourth1 = new Node(40);
    
    head1->next = second1;
    second1->prev = head1;
    second1->next = third1;
    third1->prev = second1;
    third1->next = fourth1;
    fourth1->prev = third1;
    fourth1->next = head1;  
    head1->prev = fourth1;
    
    // Second circular dll: 2 -> 3 -> 20
    Node* head2 = new Node(2);
    Node* second2 = new Node(3);
    Node* third2 = new Node(20);
    
    head2->next = second2;
    second2->prev = head2;
    second2->next = third2;
    third2->prev = second2;
    third2->next = head2;  
    head2->prev = third2;
    
    Node* res = sortedMerge(head1, head2);
    printList(res);
    
    return 0;
}
Java
// Java program to merge two sorted doubly
// circular linked lists iteratively

class Node {
    int data;
    Node next;
    Node prev;
    
    Node(int x) {
        data = x;
        next = null;
        prev = null;
    }
}

class GfG {

    // Function to merge two sorted doubly 
    // circular linked lists iteratively
    static Node sortedMerge(Node head1, Node head2) {
        
        // If either list is empty, return the other list
        if (head1 == null) return head2;
        if (head2 == null) return head1;
        
        // Find the last nodes of both lists
        Node last1 = head1.prev;
        Node last2 = head2.prev;
        
        // Create a dummy node to simplify the merging process
        Node dummy = new Node(-1);
        Node curr = dummy;
        
        // Break the circular property temporarily
        last1.next = null;
        last2.next = null;
        
        // Iterate through both linked lists
        while (head1 != null && head2 != null) {
            
            // Add the smaller node to the merged list
            if (head1.data <= head2.data) {
                curr.next = head1;
                head1.prev = curr;
                head1 = head1.next;
            } else {
                curr.next = head2;
                head2.prev = curr;
                head2 = head2.next;
            }
            curr = curr.next;
        }
        
        // If any list is left, append it to the merged list
        if (head1 != null) {
            curr.next = head1;
            head1.prev = curr;
        } else if (head2 != null) {
            curr.next = head2;
            head2.prev = curr;
        }
        
        // Find the last node of the merged list
        Node result = dummy.next;
        Node lastNode = result;
        while (lastNode.next != null) {
            lastNode = lastNode.next;
        }
        
        // Make the merged list circular
        lastNode.next = result;
        result.prev = lastNode;
        
        return result;
    }

    static void printList(Node head) {
        if (head == null) return;
        
        Node curr = head;
        do {
            System.out.print(curr.data);
            curr = curr.next;
            if (curr != head) System.out.print(" ");
        } while (curr != head);
        System.out.println();
    }

    public static void main(String[] args) {
        
        // First circular dll: 5 -> 10 -> 15 -> 40
        Node head1 = new Node(5);
        Node second1 = new Node(10);
        Node third1 = new Node(15);
        Node fourth1 = new Node(40);
        
        head1.next = second1;
        second1.prev = head1;
        second1.next = third1;
        third1.prev = second1;
        third1.next = fourth1;
        fourth1.prev = third1;
        fourth1.next = head1;  
        head1.prev = fourth1;
        
        // Second circular dll: 2 -> 3 -> 20
        Node head2 = new Node(2);
        Node second2 = new Node(3);
        Node third2 = new Node(20);
        
        head2.next = second2;
        second2.prev = head2;
        second2.next = third2;
        third2.prev = second2;
        third2.next = head2;  
        head2.prev = third2;
        
        Node res = sortedMerge(head1, head2);
        printList(res);
    }
}
Python
# Python program to merge two sorted doubly
# circular linked lists iteratively

class Node:
    def __init__(self, x):
        self.data = x
        self.next = None
        self.prev = None

# Function to merge two sorted doubly 
# circular linked lists iteratively
def sortedMerge(head1, head2):
    
    # If either list is empty, return the other list
    if head1 is None:
        return head2
    if head2 is None:
        return head1

    # Find the last nodes of both lists
    last1 = head1.prev
    last2 = head2.prev

    # Create a dummy node to simplify the merging process
    dummy = Node(-1)
    curr = dummy

    # Break the circular property temporarily
    last1.next = None
    last2.next = None

    # Iterate through both linked lists
    while head1 and head2:
        # Add the smaller node to the merged list
        if head1.data <= head2.data:
            curr.next = head1
            head1.prev = curr
            head1 = head1.next
        else:
            curr.next = head2
            head2.prev = curr
            head2 = head2.next
        curr = curr.next

    # If any list is left, append it to the merged list
    if head1:
        curr.next = head1
        head1.prev = curr
    elif head2:
        curr.next = head2
        head2.prev = curr

    # Find the last node of the merged list
    result = dummy.next
    lastNode = result
    while lastNode.next:
        lastNode = lastNode.next

    # Make the merged list circular
    lastNode.next = result
    result.prev = lastNode

    return result

def printList(head):
    if head is None:
        return
    curr = head
    while True:
        print(curr.data, end="")
        curr = curr.next
        if curr == head:
            break
        print(" ", end="")
    print()

if __name__ == "__main__":
    
    # First circular dll: 5 -> 10 -> 15 -> 40
    head1 = Node(5)
    second1 = Node(10)
    third1 = Node(15)
    fourth1 = Node(40)

    head1.next = second1
    second1.prev = head1
    second1.next = third1
    third1.prev = second1
    third1.next = fourth1
    fourth1.prev = third1
    fourth1.next = head1
    head1.prev = fourth1

    # Second circular dll: 2 -> 3 -> 20
    head2 = Node(2)
    second2 = Node(3)
    third2 = Node(20)

    head2.next = second2
    second2.prev = head2
    second2.next = third2
    third2.prev = second2
    third2.next = head2
    head2.prev = third2

    res = sortedMerge(head1, head2)
    printList(res)
C#
// C# program to merge two sorted doubly
// circular linked lists iteratively

class Node {
    public int data;
    public Node next;
    public Node prev;

    public Node(int x) {
        data = x;
        next = null;
        prev = null;
    }
}

class GfG {

    // Function to merge two sorted doubly 
    // circular linked lists iteratively
    static Node sortedMerge(Node head1, Node head2) {
        
        // If either list is empty, return the other list
        if (head1 == null) return head2;
        if (head2 == null) return head1;

        // Find the last nodes of both lists
        Node last1 = head1.prev;
        Node last2 = head2.prev;

        // Create a dummy node to simplify the merging process
        Node dummy = new Node(-1);
        Node curr = dummy;

        // Break the circular property temporarily
        last1.next = null;
        last2.next = null;

        // Iterate through both linked lists
        while (head1 != null && head2 != null) {
            // Add the smaller node to the merged list
            if (head1.data <= head2.data) {
                curr.next = head1;
                head1.prev = curr;
                head1 = head1.next;
            } else {
                curr.next = head2;
                head2.prev = curr;
                head2 = head2.next;
            }
            curr = curr.next;
        }

        // If any list is left, append it to the merged list
        if (head1 != null) {
            curr.next = head1;
            head1.prev = curr;
        } else if (head2 != null) {
            curr.next = head2;
            head2.prev = curr;
        }

        // Find the last node of the merged list
        Node result = dummy.next;
        Node lastNode = result;
        while (lastNode.next != null) {
            lastNode = lastNode.next;
        }

        // Make the merged list circular
        lastNode.next = result;
        result.prev = lastNode;

        return result;
    }

    static void printList(Node head) {
        if (head == null) return;

        Node curr = head;
        do {
            System.Console.Write(curr.data);
            curr = curr.next;
            if (curr != head) System.Console.Write(" ");
        } while (curr != head);
        System.Console.WriteLine();
    }

    static void Main(string[] args) {
        
        // First circular dll: 5 -> 10 -> 15 -> 40
        Node head1 = new Node(5);
        Node second1 = new Node(10);
        Node third1 = new Node(15);
        Node fourth1 = new Node(40);

        head1.next = second1;
        second1.prev = head1;
        second1.next = third1;
        third1.prev = second1;
        third1.next = fourth1;
        fourth1.prev = third1;
        fourth1.next = head1;
        head1.prev = fourth1;

        // Second circular dll: 2 -> 3 -> 20
        Node head2 = new Node(2);
        Node second2 = new Node(3);
        Node third2 = new Node(20);

        head2.next = second2;
        second2.prev = head2;
        second2.next = third2;
        third2.prev = second2;
        third2.next = head2;
        head2.prev = third2;

        Node res = sortedMerge(head1, head2);
        printList(res);
    }
}
JavaScript
// JavaScript program to merge two sorted doubly
// circular linked lists iteratively

class Node {
    constructor(x) {
        this.data = x;
        this.next = null;
        this.prev = null;
    }
}

// Function to merge two sorted doubly 
// circular linked lists iteratively
function sortedMerge(head1, head2) {
    
    // If either list is empty, return the other list
    if (head1 == null) return head2;
    if (head2 == null) return head1;

    // Find the last nodes of both lists
    let last1 = head1.prev;
    let last2 = head2.prev;

    // Create a dummy node to simplify the merging process
    let dummy = new Node(-1);
    let curr = dummy;

    // Break the circular property temporarily
    last1.next = null;
    last2.next = null;

    // Iterate through both linked lists
    while (head1 !== null && head2 !== null) {
        // Add the smaller node to the merged list
        if (head1.data <= head2.data) {
            curr.next = head1;
            head1.prev = curr;
            head1 = head1.next;
        } else {
            curr.next = head2;
            head2.prev = curr;
            head2 = head2.next;
        }
        curr = curr.next;
    }

    // If any list is left, append it to the merged list
    if (head1 !== null) {
        curr.next = head1;
        head1.prev = curr;
    } else if (head2 !== null) {
        curr.next = head2;
        head2.prev = curr;
    }

    // Find the last node of the merged list
    let result = dummy.next;
    let lastNode = result;
    while (lastNode.next !== null) {
        lastNode = lastNode.next;
    }

    // Make the merged list circular
    lastNode.next = result;
    result.prev = lastNode;

    return result;
}

function printList(head) {
    if (head === null) return;

    let curr = head;
    do {
        process.stdout.write(curr.data.toString());
        curr = curr.next;
        if (curr !== head) process.stdout.write(" ");
    } while (curr !== head);
    console.log();
}

// First circular dll: 5 -> 10 -> 15 -> 40
let head1 = new Node(5);
let second1 = new Node(10);
let third1 = new Node(15);
let fourth1 = new Node(40);

head1.next = second1;
second1.prev = head1;
second1.next = third1;
third1.prev = second1;
third1.next = fourth1;
fourth1.prev = third1;
fourth1.next = head1;
head1.prev = fourth1;

// Second circular dll: 2 -> 3 -> 20
let head2 = new Node(2);
let second2 = new Node(3);
let third2 = new Node(20);

head2.next = second2;
second2.prev = head2;
second2.next = third2;
third2.prev = second2;
third2.next = head2;
head2.prev = third2;

let res = sortedMerge(head1, head2);
printList(res);

Output
2 3 5 10 15 20 40

Related Article:



Next Article
Practice Tags :

Similar Reads