Find minimum and maximum elements in singly Circular Linked List
Last Updated :
21 Mar, 2023
Given a singly circular linked list of N nodes. The task is to find the smallest and largest elements in the circular linked list.
Examples:
Input : List = 99->11->22->33->44->55->66
Output : Minimum = 11, Maximum = 99
Input : List = 12->11->9->33->125->6->99
Output : Minimum = 6, Maximum = 125
The idea is to traverse the circular linked list while the last node is not reached and initialize the max and min variables to INT_MIN and INT_MAX respectively. After that, check the condition that if the max value is less than the head value, then the head value is assigned to the max or if the min value is greater than the head value, then the head value is assigned to the min, otherwise the head point to the next node. Continue this process until the last node.
Algorithm:
Step 1: Create a function named "printMinMax" that takes the head node of a circular linked list as an input parameter.
Step 2: Create two integer variables min and max and initialize them to Integer.MAX_VALUE and Integer.MIN_VALUE, respectively.
Step 3: Define a pointer "current" and initialize it with the head node.
Step 4: Start a while loop till current.next is not equal to null.
a. In each iteration of the while loop, compare the data of the current node with the values of min and max. If the data is lesser than the value of min, assign the data to the min variable. If the data is greater than the value of max, assign the data to the max variable.
b. Update the current pointer to the next node.
Step 5: Now after the while loop, print the values of min and max statement.
Step 6: Create a function named "insertNode" which takes a head node of a circular linked list and a data value to be inserted into the linked list an input parameter.
Step 7: Create a new node with the given data value and check whether the node is created successfully or not.
Step 8: Assign the new node's next pointer to the node itself if the linked list is empty, and then return the new node as the head node.
Step 9: If the linked list is not empty, traverse the linked list until the last node using a while loop.
Step 10: Give the head node the new node's next pointer and then give it back.
Step 11: Create a function named "displayList" which take the head node of a circular linked list and prints the values of all the nodes in the linked list as an input parameter.
Step 12: Use a do-while loop to iteratively navigate the linked list until you reach the head node once more after initializing a pointer with the head node.
Step 13: Print the value of the current node using the System on each iteration of the do-while loop. out. move the current pointer to the following node and use the print statement
Below is the implementation of the above approach:
C++
// C++ program to find minimum and maximum
// value from singly circular linked list
#include <bits/stdc++.h>
using namespace std;
// structure for a node
struct Node {
int data;
struct Node* next;
};
// Function to print minimum and maximum
// nodes of the circular linked list
void printMinMax(struct Node** head)
{
// check list is empty
if (*head == NULL) {
return;
}
// pointer for traversing
struct Node* current;
// initialize head to current pointer
current = *head;
// initialize max int value to min
// initialize min int value to max
int min = INT_MAX, max = INT_MIN;
// While last node is not reached
do {
// If current node data is lesser for min
// then replace it
if (current->data < min) {
min = current->data;
}
// If current node data is greater for max
// then replace it
if (current->data > max) {
max = current->data;
}
current = current->next;
}
while(current != *head);
cout << "\nMinimum = " << min << ", Maximum = " << max;
}
// Function to insert a node at the end of
// a Circular linked list
void insertNode(struct Node** head, int data)
{
struct Node* current = *head;
// Create a new node
struct Node* newNode = new Node;
// check node is created or not
if (!newNode) {
printf("\nMemory Error\n");
return;
}
// insert data into newly created node
newNode->data = data;
// check list is empty
// if not have any node then
// make first node it
if (*head == NULL) {
newNode->next = newNode;
*head = newNode;
return;
}
// if list have already some node
else {
// move first node to last node
while (current->next != *head) {
current = current->next;
}
// put first or head node address
// in new node link
newNode->next = *head;
// put new node address into
// last node link(next)
current->next = newNode;
}
}
// Function to print the Circular linked list
void displayList(struct Node* head)
{
struct Node* current = head;
// if list is empty simply show message
if (head == NULL) {
printf("\nDisplay List is empty\n");
return;
}
// traverse first to last node
else {
do {
printf("%d ", current->data);
current = current->next;
} while (current != head);
}
}
// Driver Code
int main()
{
struct Node* Head = NULL;
insertNode(&Head, 99);
insertNode(&Head, 11);
insertNode(&Head, 22);
insertNode(&Head, 33);
insertNode(&Head, 44);
insertNode(&Head, 55);
insertNode(&Head, 66);
cout << "Initial List: ";
displayList(Head);
printMinMax(&Head);
return 0;
}
Java
// Java program to find minimum and maximum
// value from singly circular linked list
class GFG
{
// structure for a node
static class Node
{
int data;
Node next;
};
// Function to print minimum and maximum
// nodes of the circular linked list
static void printMinMax(Node head)
{
// check list is empty
if (head == null)
{
return;
}
// pointer for traversing
Node current;
// initialize head to current pointer
current = head;
// initialize max int value to min
// initialize min int value to max
int min = Integer.MAX_VALUE, max = Integer.MIN_VALUE;
// While last node is not reached
while (current.next != head)
{
// If current node data is lesser for min
// then replace it
if (current.data < min)
{
min = current.data;
}
// If current node data is greater for max
// then replace it
if (current.data > max)
{
max = current.data;
}
current = current.next;
}
System.out.println( "\nMinimum = " + min + ", Maximum = " + max);
}
// Function to insert a node at the end of
// a Circular linked list
static Node insertNode(Node head, int data)
{
Node current = head;
// Create a new node
Node newNode = new Node();
// check node is created or not
if (newNode == null)
{
System.out.printf("\nMemory Error\n");
return null;
}
// insert data into newly created node
newNode.data = data;
// check list is empty
// if not have any node then
// make first node it
if (head == null)
{
newNode.next = newNode;
head = newNode;
return head;
}
// if list have already some node
else
{
// move first node to last node
while (current.next != head)
{
current = current.next;
}
// put first or head node address
// in new node link
newNode.next = head;
// put new node address into
// last node link(next)
current.next = newNode;
}
return head;
}
// Function to print the Circular linked list
static void displayList(Node head)
{
Node current = head;
// if list is empty simply show message
if (head == null)
{
System.out.printf("\nDisplay List is empty\n");
return;
}
// traverse first to last node
else
{
do
{
System.out.printf("%d ", current.data);
current = current.next;
} while (current != head);
}
}
// Driver Code
public static void main(String args[])
{
Node Head = null;
Head=insertNode(Head, 99);
Head=insertNode(Head, 11);
Head=insertNode(Head, 22);
Head=insertNode(Head, 33);
Head=insertNode(Head, 44);
Head=insertNode(Head, 55);
Head=insertNode(Head, 66);
System.out.println("Initial List: ");
displayList(Head);
printMinMax(Head);
}
}
// This code is contributed by Arnab Kundu
Python3
# Python3 program to find minimum and maximum
# value from singly circular linked list
# structure for a node
class Node:
def __init__(self):
self.data = 0
self.next = None
# Function to print minimum and maximum
# nodes of the circular linked list
def printMinMax(head):
# check list is empty
if (head == None):
return;
# initialize head to current pointer
current = head;
# initialize max int value to min
# initialize min int value to max
min = 1000000000
max = -1000000000;
# While last node is not reached
while True:
# If current node data is lesser for min
# then replace it
if (current.data < min):
min = current.data;
# If current node data is greater for max
# then replace it
if (current.data > max):
max = current.data;
current = current.next;
if(current == head):
break
print('Minimum = {}, Maximum = {}'.format(min, max))
# Function to insert a node at the end of
# a Circular linked list
def insertNode(head, data):
current = head;
# Create a new node
newNode = Node()
# check node is created or not
if not newNode:
print("\nMemory Error");
return head
# insert data into newly created node
newNode.data = data;
# check list is empty
# if not have any node then
# make first node it
if (head == None):
newNode.next = newNode;
head = newNode;
return head
# if list have already some node
else:
# move first node to last node
while (current.next != head):
current = current.next;
# put first or head node address
# in new node link
newNode.next = head;
# put new node address into
# last node link(next)
current.next = newNode;
return head
# Function to print the Circular linked list
def displayList(head):
current = head;
# if list is empty simply show message
if (head == None):
print("\nDisplay List is empty");
return;
# traverse first to last node
else:
while True:
print(current.data, end=' ');
current = current.next;
if(current==head):
break
print()
# Driver Code
if __name__=='__main__':
Head = None;
Head = insertNode(Head, 99);
Head = insertNode(Head, 11);
Head = insertNode(Head, 22);
Head = insertNode(Head, 33);
Head = insertNode(Head, 44);
Head = insertNode(Head, 55);
Head = insertNode(Head, 66);
print("Initial List: ", end = '')
displayList(Head);
printMinMax(Head);
# This code is contributed by rutvik_56
C#
// C# program to find minimum and maximum
// value from singly circular linked list
using System;
class GFG
{
// structure for a node
public class Node
{
public int data;
public Node next;
};
// Function to print minimum and maximum
// nodes of the circular linked list
static void printMinMax(Node head)
{
// check list is empty
if (head == null)
{
return;
}
// pointer for traversing
Node current;
// initialize head to current pointer
current = head;
// initialize max int value to min
// initialize min int value to max
int min = int.MaxValue, max = int.MinValue;
// While last node is not reached
while (current.next != head)
{
// If current node data is lesser for min
// then replace it
if (current.data < min)
{
min = current.data;
}
// If current node data is greater for max
// then replace it
if (current.data > max)
{
max = current.data;
}
current = current.next;
}
Console.WriteLine( "\nMinimum = " + min + ", Maximum = " + max);
}
// Function to insert a node at the end of
// a Circular linked list
static Node insertNode(Node head, int data)
{
Node current = head;
// Create a new node
Node newNode = new Node();
// check node is created or not
if (newNode == null)
{
Console.Write("\nMemory Error\n");
return null;
}
// insert data into newly created node
newNode.data = data;
// check list is empty
// if not have any node then
// make first node it
if (head == null)
{
newNode.next = newNode;
head = newNode;
return head;
}
// if list have already some node
else
{
// move first node to last node
while (current.next != head)
{
current = current.next;
}
// put first or head node address
// in new node link
newNode.next = head;
// put new node address into
// last node link(next)
current.next = newNode;
}
return head;
}
// Function to print the Circular linked list
static void displayList(Node head)
{
Node current = head;
// if list is empty simply show message
if (head == null)
{
Console.Write("\nDisplay List is empty\n");
return;
}
// traverse first to last node
else
{
do
{
Console.Write("{0} ", current.data);
current = current.next;
} while (current != head);
}
}
// Driver Code
public static void Main()
{
Node Head = null;
Head=insertNode(Head, 99);
Head=insertNode(Head, 11);
Head=insertNode(Head, 22);
Head=insertNode(Head, 33);
Head=insertNode(Head, 44);
Head=insertNode(Head, 55);
Head=insertNode(Head, 66);
Console.WriteLine("Initial List: ");
displayList(Head);
printMinMax(Head);
}
}
/* This code contributed by PrinciRaj1992 */
JavaScript
<script>
// javascript program to find minimum and maximum
// value from singly circular linked list
// structure for a node
class Node {
constructor() {
this.data = 0;
this.next = null;
}
}
// Function to print minimum and maximum
// nodes of the circular linked list
function printMinMax(head) {
// check list is empty
if (head == null) {
return;
}
// pointer for traversing
var current;
// initialize head to current pointer
current = head;
// initialize max var value to min
// initialize min var value to max
var min = Number.MAX_VALUE, max = Number.MIN_VALUE;
// While last node is not reached
while (current.next != head) {
// If current node data is lesser for min
// then replace it
if (current.data < min) {
min = current.data;
}
// If current node data is greater for max
// then replace it
if (current.data > max) {
max = current.data;
}
current = current.next;
}
document.write("<br/>Minimum = " + min + ", Maximum = " + max);
}
// Function to insert a node at the end of
// a Circular linked list
function insertNode(head , data) {
var current = head;
// Create a new node
var newNode = new Node();
// check node is created or not
if (newNode == null) {
document.write("<br/>Memory Error\n");
return null;
}
// insert data into newly created node
newNode.data = data;
// check list is empty
// if not have any node then
// make first node it
if (head == null) {
newNode.next = newNode;
head = newNode;
return head;
}
// if list have already some node
else {
// move first node to last node
while (current.next != head) {
current = current.next;
}
// put first or head node address
// in new node link
newNode.next = head;
// put new node address into
// last node link(next)
current.next = newNode;
}
return head;
}
// Function to print the Circular linked list
function displayList(head) {
var current = head;
// if list is empty simply show message
if (head == null) {
document.write("<br/>Display List is empty<br/>");
return;
}
// traverse first to last node
else {
do {
document.write(current.data+" ");
current = current.next;
} while (current != head);
}
}
// Driver Code
var Head = null;
Head = insertNode(Head, 99);
Head = insertNode(Head, 11);
Head = insertNode(Head, 22);
Head = insertNode(Head, 33);
Head = insertNode(Head, 44);
Head = insertNode(Head, 55);
Head = insertNode(Head, 66);
document.write("Initial List: ");
displayList(Head);
printMinMax(Head);
// This code contributed by umadevi9616
</script>
OutputInitial List: 99 11 22 33 44 55 66
Minimum = 11, Maximum = 99
Complexity Analysis:
- Time Complexity: O(n), as we are using a loop to traverse n times. Where n is the number of nodes in the linked list.
- Auxiliary Space: O(1), as we are not using any extra space.
Similar Reads
Count minimum frequency elements in a linked list
Given a linked list containing duplicate elements. The task is to find the count of all minimum occurring elements in the given linked list. That is the count of all such elements whose frequency is minimum in the matrix. Examples: Input : 1-> 2-> 2-> 3 Output : 2 Explanation: 1 and 3 are e
8 min read
Maximum and Minimum element of a linked list which is divisible by a given number k
Given a singly linked list of N nodes. Find the smallest and largest elements in linked list divisible by a given number K . Examples: Input : List = 15 -> 14 -> 13 -> 22 -> 50 , K = 5 Output : Maximum element in linked list divisible by K: 50 Minimum element in linked list divisible by
10 min read
Convert singly linked list into circular linked list
Given a singly linked list, the task is to convert it into a circular linked list.Examples:Input: head: 1 -> 2 -> 3 -> 4 -> 5 -> 6 -> NULLOutput: Explanation: Singly linked list is converted to circular by pointing last node to head.Input: head: 2 -> 4 -> 6 -> 8 -> 10 -
11 min read
Delete all Prime Nodes from a Circular Singly Linked List
Given a circular singly linked list containing N nodes. The task is to delete all nodes from the list which are prime. Examples: Input : 9->11->32->6->13->20 Output : 9 32 6 20 Input : 6->11->16->21->17->10 Output : 6 16 21 10 Approach: The idea is to traverse the nodes
13 min read
Minimum and Maximum Prime Numbers of a Singly Linked List
Given a singly linked list containing N nodes, the task is to find the minimum and maximum prime number. Examples: Input : List = 15 -> 16 -> 6 -> 7 -> 17 Output : Minimum : 7 Maximum : 17 Input : List = 15 -> 3 -> 4 -> 2 -> 9 Output : Minimum : 2 Maximum : 3 Approach: The id
9 min read
Next Greater Element in a Circular Linked List
Given a circular singly linked list, the task is to print the next greater element for each node in the linked list. If there is no next greater element for any node, then print "-1" for that node. Examples: Input: head = 1 ? 5 ? 2 ? 10 ? 0 ? (head)Output: 5 10 10 -1 -1Explanation:The next greater e
11 min read
Find smallest and largest elements in singly linked list
Given a singly linked list of n nodes, the task is to find the smallest and largest element in linked list.Examples: Input: 15 -> 14 -> 13 -> 22 -> 17Output: 13 22 Explanation: The minimum element in the linked list is 13, and the maximum element is 22. Input: 20 -> 25 -> 23 ->
7 min read
Insertion in Circular Singly Linked List
In this article, we will learn how to insert a node into a circular linked list. Insertion is a fundamental operation in linked lists that involves adding a new node to the list. In a circular linked list, the last node connects back to the first node, creating a loop.There are four main ways to add
12 min read
Circular Linked List meaning in DSA
A circular linked list is a special type of linked list in which the last node is connected to the first node, creating a continuous loop. In a circular linked list, each node has a reference to the next node in the sequence, just like in a regular linked list, but the last node's reference points b
3 min read
Search an Element in Doubly Circular Linked List
Pre-requisite: Convert an Array to a Circular Doubly Linked List, Doubly Circular Linked ListGiven a doubly circular linked list. The task is to find the position of an element in the list. Image Representation: Algorithm: Declare a temp pointer, and initialize it to the head of the list.Iterate the
13 min read