# Python program to find the maximum and minimum value node from a circular linked list

PythonServer Side ProgrammingProgramming

When it is required to find the maximum and minimum node values from a circular linked list, a 'Node' class needs to be created. In this class, there are two attributes, the data that is present in the node, and the access to the next node of the linked list.

In a circular linked list, the head and the rear are adjacent to each other. They are connected to form a circle, and don't have 'NULL' value in the last node.

Another class needs to be created that would have an initialization function, and the head of the node would be initialized to 'None'.

Multiple methods are defined by the user to add node to the linked list, to find the minimum and maximum values in the nodes and display them.

Below is a demonstration for the same −

## Example

Live Demo

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

class list_creation:
def __init__(self):
self.tail = Node(None)

new_node = Node(my_data)
self.tail = new_node
else:
self.tail.next = new_node
self.tail = new_node

def find_min_node(self):
print("The list is empty");
else:
while(True):
if(min_val > curr.data):
min_val = curr.data;
curr= curr.next;
break;
print("Minimum value node in the list: "+ str(min_val));

def find_max_node(self):
print("List is empty");
else:
while(True):
if(max_val < curr.data):
max_val = curr.data;
curr= curr.next;
break;
print("The maximum valueed node is : "+ str(max_val));

my_cl = list_creation()
print("Values have been added to the list")
my_cl.find_max_node()
my_cl.find_min_node()

## Output

Values have been added to the list
The maximum valueed node is : 74
Minimum value node in the list: 11

## Explanation

• The 'Node' class is created.
• Another class with required attributes is created.
• Another method named 'add_data' is defined, that is used to add data to the circular linked list.
• Another method named 'find_max_node' is defined, that iterates through the list and gets the maximum value in a node.
• Another method named 'find_min_node' is defined, that iterates through the list and gets the minimum value in a node.
• An object of the 'list_creation' class is created, and the methods are called on it to add data.
• An 'init' method is defined, that the first and last nodes of the circular linked list to None.
• The 'find_max_node' method and the 'find_min_node' methods are called.
• It iterates through the nodes in the linked list, gets the maximum and the minimum value in the list.
• This is displayed on the console.
Published on 13-Mar-2021 06:00:13