
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
Insert into a Sorted Circular Linked List in C++
Suppose we have a node from a Circular Linked List which is sorted in increasing order, we have to define a function to insert a value insertVal into the list such that it remains a sorted circular list.
The node can be a reference to any single node in the list, and may not be necessarily the first value of the circular list. If there are multiple suitable places for insertion, we can choose any place to insert the new value. If the list is empty, then we have to create a new single circular list and return the reference to that single node. Otherwise, we should return the original given node.
So, if the input is like head = [3,4,1], insertVal = 2, image, then the output will be [3,4,1,2], image
To solve this, we will follow these steps −
if head is null, then −
head := new node with val
next of head := head
Otherwise
curr = next of head
prev = head
temp = new node with val
done := false
Do infinite looping, do −
if val in curr >= val and val of prev <= val, then −
prev := next of temp
temp := next of curr
done := true
Come out from the loop
if val of prev > val of curr, then −
if val of prev <= val or val <= val of curr, then −
prev := next of temp
temp := next of curr
done := true
Come out from the loop
if curr is same as head, then −
Come out from the loop
prev := curr
curr := next of curr
if done is false, then −
temp := next of head
prev := next of temp
head := temp
return head
Example
Let us see the following implementation to get a better understanding −
#include <bits/stdc++.h> using namespace std; class Node { public: int val; Node* next; Node() {} Node(int _val) { val = _val; next = NULL; } Node(int _val, Node* _next) { val = _val; next = _next; } }; class Solution { public: Node* insert(Node* head, int val) { if(!head){ head = new Node(val); head->next = head; } else{ Node* curr = head->next; Node* prev = head; Node* temp = new Node(val); bool done = false; while(1){ if (curr->val >= val && prev->val <= val) { prev->next = temp; temp->next = curr; done = true; break; } if (prev->val > curr->val) { if (prev->val <= val || val <= curr->val) { prev->next = temp; temp->next = curr; done = true; break; } } if (curr == head) break; prev = curr; curr = curr->next; } if(!done){ temp->next = head; prev->next = temp; head = temp; } } return head; } }; main(){ Solution ob; Node *head = new Node(3); head->next = new Node(4); head->next->next = new Node(1, head); ob.insert(head, 2); Node *temp = head; if (head != NULL){ do{ cout << temp->val << " "; temp = temp->next; } while (temp != head); } }
Input
node *head = new Node(3); head->next = new Node(4); head->next->next = new Node(1, head); insertVal = 2
Output
3 4 1 2
- Related Articles
- Convert singly linked list into circular linked list in C++
- Check if a linked list is Circular Linked List in C++
- Finding Median in a Sorted Linked List in C++
- Python program to insert an element into sorted list
- Convert singly linked list into XOR linked list in C++
- Count nodes in Circular linked list in C++
- Check if a Linked List is Pairwise Sorted in C++
- C++ Program to Implement Sorted Doubly Linked List
- C++ Program to Implement Sorted Singly Linked List
- Python program to insert a new node at the beginning of the Circular Linked List
- Python program to insert a new node at the end of the Circular Linked List
- Python program to insert a new node at the middle of the Circular Linked List
- Count rotations in sorted and rotated linked list in C++
- Convert an Array to a Circular Doubly Linked List in C++
- Sum of the nodes of a Circular Linked List in C++
