Insertion in a sorted circular linked list when a random pointer is given

Given an array arr[] of integers and a pointer to a random node of a circular sorted linked list (initially empty), the task is to insert all the elements of arr[] in the circular linked list.

Examples:

Input: arr[] = {12, 56, 2, 11, 1, 90}
Output: 1 2 11 12 56 90



Input: arr[] = {6, 2, 78, 45, 200}
Output: 2 6 45 78 200

Approach: We are given a random pointer to a node in the circular linked list and we have to find the head of the circular linked list to insert the node in a sorted linked list.
Insertion in a sorted linked list when head is given is explained in this article.
To find the head of the circular sorted linked list:

  • Find the last node of linked list (the last node will be greater than its successor i.e. first element.
  • Once the head of the linked list is found then the elements can be inserted using the same approach which is discussed in the previous article.

Below is the implementation of the above approach:

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ implementation of the approach
#include <bits/stdc++.h>
using namespace std;
  
// Node structure
struct Node {
    Node* next;
    int data;
};
  
// Function to create a node
Node* create()
{
    Node* new_node = (Node*)malloc(sizeof(Node));
    new_node->next = NULL;
  
    return new_node;
}
  
// Function to find and return the head
Node* find_head(Node* random)
{
    // If the list is empty
    if (random == NULL)
        return NULL;
  
    Node *head, *var = random;
  
    // Finding the last node of the linked list
    // the last node must have the highest value
    // if no such element is present then all the nodes
    // of the linked list must be same
    while (!(var->data > var->next->data || var->next == random)) {
        var = var->next;
    }
  
    // Return the head
    return var->next;
}
  
// Function to insert a new_node in the list in sorted fashion
// Note that this function expects a pointer to head node
// as this can modify the head of the input linked list
Node* sortedInsert(Node* head_ref, Node* new_node)
{
    Node* current = head_ref;
  
    // If the list is empty
    if (current == NULL) {
        new_node->next = new_node;
        head_ref = new_node;
    }
  
    // If the node to be inserted is the smallest
    // then it has to be the new head
    else if (current->data >= new_node->data) {
  
        // Find the last node of the list as it
        // will be pointing to the head
        while (current->next != head_ref)
            current = current->next;
        current->next = new_node;
        new_node->next = head_ref;
        head_ref = new_node;
    }
  
    else {
        // Locate the node before the point of insertion
        while (current->next != head_ref
               && current->next->data < new_node->data) {
            current = current->next;
        }
  
        new_node->next = current->next;
        current->next = new_node;
    }
  
    // Return the new head
    return head_ref;
}
  
// Function to print the nodes of the linked list
void printList(Node* start)
{
    Node* temp;
  
    if (start != NULL) {
        temp = start;
        do {
            cout << temp->data << " ";
            temp = temp->next;
        } while (temp != start);
    }
}
  
// Driver code
int main()
{
  
    int arr[] = { 12, 56, 2, 11, 1, 90 };
    int list_size, i;
  
    // Start with an empty linked list
    Node* start = NULL;
    Node* temp;
  
    // Create linked list from the given array
    for (i = 0; i < 6; i++) {
  
        // Move to a random node if it exists
        if (start != NULL)
            for (int j = 0; j < (rand() % 10); j++)
                start = start->next;
  
        temp = create();
        temp->data = arr[i];
        start = sortedInsert(find_head(start), temp);
    }
  
    // Print the contents of the created list
    printList(find_head(start));
  
    return 0;
}

chevron_right


Output:

1 2 11 12 56 90


My Personal Notes arrow_drop_up

Second year Department of Information Technology Jadavpur University

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.