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

python tutorials and learn python

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

In this program, we will create a circular linked list then, iterate through the list to find out the minimum and maximum node.

1. 9->5->2->7->3

We will maintain two variables min and max. Min will hold the minimum value node, and max will hold the maximum value node. In the above example, 2 will be the minimum value node and 9 will be the maximum value node.

### ALGORITHM:

1. Define a Node class which represents a node in the list. It has two properties data and next which will point to the next node.
2. Define another class for creating the circular linked list, and it has two nodes: head and tail.
3. minNode() will print out minimum value node:
• Define variable min and initialize with head’s data.
• Current will point to head.
• Iterate through the list by comparing each node’s data with min.
• If min > current’s data then min will hold current’s data.
• At the end of the list, variable min will hold the minimum value node.
• Print the min value.

a. maxNode() will prints out maximum value node:

• Define variable max and initialize with head’s data.
• Current will point to head.
• Iterate through the list by comparing each node’s data with max.
• If max > current’s data, then max will hold current’s data.
• At the end of the list, variable max will hold the maximum value node.
• Print the max value.

### PROGRAM:

1. #Represents the node of list.
2. class Node:
3.     def __init__(self,data):
4.         self.data = data;
5.         self.next = None;
6. class CreateList:
7.     #Declaring head and tail pointer as null.
8.     def __init__(self):
10.         self.tail = Node(None);
13.     #This function will add the new node at the end of the list.
15.         newNode = Node(data);
16.         #Checks if the list is empty.
18.             #If list is empty, both head and tail would point to new node.
20.             self.tail = newNode;
22.         else:
23.             #tail will point to new node.
24.             self.tail.next = newNode;
25.             #New node will become new tail.
26.             self.tail = newNode;
27.             #Since, it is circular linked list tail will point to head.
29.     #Finds out the minimum value node in the list
30.     def minNode(self):
32.         #Initializing min to initial node data
35.             print(“List is empty”);
36.         else:
37.             while(True):
38.                 #If current node’s data is smaller than min
39.                 #Then replace value of min with current node’s data
40.                 if(minimum > current.data):
41.                     minimum = current.data;
42.                 current= current.next;
44.                     break;
45.         print(“Minimum value node in the list: “+ str(minimum));
46.     #Finds out the maximum value node in the list
47.     def maxNode(self):
49.         #Initializing max to initial node data
52.             print(“List is empty”);
53.         else:
54.             while(True):
55.                 #If current node’s data is greater than max
56.                 #Then replace value of max with current node’s data
57.                 if(maximum < current.data):
58.                     maximum = current.data;
59.                 current= current.next;
61.                     break;
62.         print(“Maximum value node in the list: “+ str(maximum));
64.     cl = CreateList();
65.     #Adds data to the list
```Minimum value node in the list: 1