# Sorted merge of two sorted circular linked lists

The Sorted Merge of Two Sorted Circular Linked Listsproblem involves merging two circular linked lists that are already sorted in ascending order, while maintaining the sorted order of the merged list. A circular linked list is a data structure where the last node points back to the first node, creating a loop. The task is to merge the two circular linked lists into a single circular linked list while keeping the elements sorted.

## Problem Statement

Given two circular linked lists that are already sorted, the problem is to merge these two lists into a single circular linked list while maintaining the sorted order.

## Example

Consider two sorted circular linked lists:

• First Linked List: 2 -> 5 -> 7 -> 10 -> 11 -> 17
• Second Linked List: 1 -> 3 -> 4 -> 8 -> 20

The merged circular linked list should be: 1 -> 2 -> 3 -> 4 -> 5 -> 7 -> 8 -> 10 -> 11 -> 17 -> 20.

## Idea to Solve the Problem

To solve the problem of merging two sorted circular linked lists, follow these steps:

1. Create a new circular linked list to store the merged result.
2. Initialize pointers `a` and `b` to the first nodes of the two input circular linked lists.
3. Compare the values of `a` and `b` nodes and append the smaller value node to the merged list.
4. Move the pointer of the chosen node to its next.
5. Repeat steps 3-4 until both lists are exhausted.
6. If one of the lists still has remaining nodes, append the remaining nodes to the merged list.

## Pseudocode

``````function sortedMerge(l1, l2):
auxiliary = null
while a is not null and b is not null:
if a.data < b.data:
auxiliary = a
a = a.next
else:
auxiliary = b
b = b.next
if result is not null:
result.tail.next = auxiliary
result.tail = auxiliary
auxiliary = null
if b is not null:
result.tail.next = b
b = b.next
l1.tail = b
else if a is not null:
result.tail.next = a
return result``````

## Algorithm Explanation

1. Initialize two pointers `a` and `b` to the first nodes of the input lists.
2. Create a new circular linked list `result` to store the merged output.
3. Compare the values of `a` and `b`. Append the smaller node to the `result` list.
4. Move the pointer of the chosen node (`a` or `b`) to its next.
5. Repeat the above steps until either `a` or `b` becomes null.
6. If either `a` or `b` is still not null, append the remaining nodes to the `result` list.
7. Update the `next` pointers to maintain the circular structure.

## Resultant Output Explanation

After merging the given sorted circular linked lists using the provided code, the merged circular linked list is printed. The output demonstrates that the elements from both input lists have been merged and the resulting list is in sorted order.

## Time Complexity

The time complexity of merging two sorted circular linked lists is O(n + m), where n and m are the lengths of the input circular linked lists. This is because, in the worst case, we may need to iterate through both input lists once to merge them.

## Comment

Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.