# Program To Find The Maximum And Minimum Value Node From A Singly Linked List

Program To Find The Maximum And Minimum Value Node From A Singly Linked List on fibonacci, factorial, prime, armstrong, swap, reverse, search, sort, stack, queue, array, linkedlist, tree, graph etc.

## Program to find the maximum and minimum value node from a singly linked list

### Explanation

In this program, we need to find out the minimum and maximum value node in the given singly linked list.

We will maintain two variables min and max. Min will hold minimum value node, and max will hold maximum value node. In the above example, 1 will be minimum value node and 8 will be maximum value node. The algorithm to find the maximum and minimum node is given below.

### Algorithm

1. Create a class Node which has two attributes: data and next. Next is a pointer to the next node in the list.
2. Create another class MinMax which has two attributes: head and tail.
1. Create a new node.
2. It first checks, whether the head is equal to null which means the list is empty.
3. If the list is empty, both head and tail will point to a newly added node.
4. If the list is not empty, the new node will be added to end of the list such that tail's next will point to a newly added node. This new node will become the new tail of the list.
4. minNode() will display minimum value node:
1. Define a variable min and initialize it with head's data.
2. Node current will point to head.
3. Iterate through the list by comparing each node's data with min.
4. If min is greater than current's data then, min will hold current's data.
5. At the end of the list, variable min will hold minimum value node.
6. Display the min value.
5. maxNode() will display maximum value node:
1. Define a variable max and initialize it with head's data.
2. Node current will point to head.
3. Iterate through the list by comparing each node's data with max.
4. If max is less than current's data then, max will hold current's data.
5. At the end of the list, variable max will hold maximum value node.
6. Display the max value.

### Python

```#Represent a node of the singly linked list
class Node:
def __init__(self,data):
self.data = data;
self.next = None;

class MinMax:
def __init__(self):
self.tail = None;

#Create a new node
newNode = Node(data);

#Checks if the list is empty
#If list is empty, both head and tail will point to new node
self.tail = newNode;
else:
#newNode will be added after tail such that tail's next will point to newNode
self.tail.next = newNode;
#newNode will become new tail of the list
self.tail = newNode;

#minNode() will find out the minimum value node in the list
def minNode(self):

print("List is empty");
else:
#Initializing min with head node data

while(current != None):
#If current node's data is smaller than min
#Then, replace value of min with current node's data
if(min > current.data):
min = current.data;
current = current.next;
print("Minimum value node in the list: " + str(min));

#maxNode() will find out the maximum value node in the List
def maxNode(self):

print("List is empty");
else:
#Initializing max with head node data

while(current != None):
#If current node's data is greater than max
#Then, replace value of max with current node's data
if(max < current.data):
max = current.data;
current= current.next;
print("Maximum value node in the list: " + str(max));

sList = MinMax();

#Display the minimum value node in the list
sList.minNode();

#Display the maximum value node in the list
sList.maxNode();
```

Output:

``` Minimum value node in the list: 1
Maximum value node in the list: 8
```

### C

```#include <stdio.h>

//Represent a node of the singly linked list
struct node{
int data;
struct node *next;
};

struct node *head, *tail = NULL;

//Create a new node
struct node *newNode = (struct node*)malloc(sizeof(struct node));
newNode->data = data;
newNode->next = NULL;

//Checks if the list is empty
//If list is empty, both head and tail will point to new node
tail = newNode;
}
else {
//newNode will be added after tail such that tail's next will point to newNode
tail->next = newNode;
//newNode will become new tail of the list
tail = newNode;
}
}

//minNode() will find out the minimum value node in the list
void minNode() {
int min;

printf("List is empty \n");
}
else {
//Initializing min with head node data

while(current != NULL){
//If current node's data is smaller than min
//Then, replace value of min with current node's data
if(min > current->data) {
min = current->data;
}
current= current->next;
}
printf("Minimum value node in the list: %d\n", min);
}
}

//maxNode() will find out the maximum value node in the list
void maxNode() {
int max;

printf("List is empty \n");
}
else {
//Initializing max with head node data

while(current != NULL){
//If current node's data is greater than max
//Then, replace value of max with current node's data
if(max < current->data) {
max = current->data;
}
current = current->next;
}
printf("Maximum value node in the list: %d\n", max);
}
}

int main()
{

//Display the minimum value node in the list
minNode();

//Display the maximum value node in the list
maxNode();

return 0;
}
```

Output:

```Minimum value node in the list: 1
Maximum value node in the list: 8
```

### JAVA

```public class MinMax {

//Represent a node of the singly linked list
class Node{
int data;
Node next;

public Node(int data) {
this.data = data;
this.next = null;
}
}

public Node tail = null;

//Create a new node
Node newNode = new Node(data);

//Checks if the list is empty
//If list is empty, both head and tail will point to new node
tail = newNode;
}
else {
//newNode will be added after tail such that tail's next will point to newNode
tail.next = newNode;
//newNode will become new tail of the list
tail = newNode;
}
}

//minNode() will find out the minimum value node in the list
public void minNode() {
int min;

System.out.println("List is empty");
}
else {
//Initializing min with head node data

while(current != null){
//If current node's data is smaller than min
//Then, replace value of min with current node's data
if(min > current.data) {
min = current.data;
}
current= current.next;
}
System.out.println("Minimum value node in the list: "+ min);
}
}

//maxNode() will find out the maximum value node in the list
public void maxNode() {
int max;

System.out.println("List is empty");
}
else {
//Initializing max with head node data

while(current != null){
//If current node's data is greater than max
//Then, replace value of max with current node's data
if(max < current.data) {
max = current.data;
}
current = current.next;
}
System.out.println("Maximum value node in the list: "+ max);
}
}

public static void main(String[] args) {

MinMax sList = new MinMax();

//Display the minimum value node in the list
sList.minNode();

//Display the maximum value node in the list
sList.maxNode();
}
}
```

Output:

```Minimum value node in the list: 1
Maximum value node in the list: 8
```

### C#

```using System;

public class CreateList
{
//Represent a node of the singly linked list
public class Node<T>{
public T data;
public Node<T> next;

public Node(T value) {
data = value;
next = null;
}
}

public class MinMax<T> where T : IComparable<T>{
public Node<T> tail = null;

//Create a new node
Node<T> newNode = new Node<T>(data);

//Checks if the list is empty
//If list is empty, both head and tail will point to new node
tail = newNode;
}
else {
//newNode will be added after tail such that tail's next will point to newNode
tail.next = newNode;
//newNode will become new tail of the list
tail = newNode;
}
}

//minNode() will find out the minimum value node in the list
public void minNode() {
T min;

Console.WriteLine("List is empty");
}
else {
//Initializing min with head node data

while(current != null){
//If current node's data is smaller than min
//Then, replace value of min with current node's data
if(min.CompareTo(current.data) > 0) {
min = current.data;
}
current= current.next;
}
Console.WriteLine("Minimum value node in the list: "+ min);
}
}

//maxNode() will find out the maximum value node in the list
public void maxNode() {
T max;

Console.WriteLine("List is empty");
}
else {
//Initializing max with head node data

while(current != null){
//If current node's data is greater than max
//Then, replace value of max with current node's data
if(max.CompareTo(current.data) < 0) {
max = current.data;
}
current = current.next;
}
Console.WriteLine("Maximum value node in the list: "+ max);
}
}
}

public static void Main()
{
MinMax<int> sList = new MinMax<int>();

//Display the minimum value node in the list
sList.minNode();

//Display the maximum value node in the list
sList.maxNode();
}
}
```

Output:

```Minimum value node in the list: 1
Maximum value node in the list: 8
```

### PHP

```<!DOCTYPE html>
<html>
<body>
<?php
//Represent a node of singly linked list
class Node{
public \$data;
public \$next;

function __construct(\$data){
\$this->data = \$data;
\$this->next = NULL;
}
}
class MinMax{
public \$tail;
function __construct(){
\$this->tail = NULL;
}

//Create a new node
\$newNode = new Node(\$data);

//Checks if the list is empty
//If list is empty, both head and tail will point to new node
\$this->tail = \$newNode;
}
else {
//newNode will be added after tail such that tail's next will point to newNode
\$this->tail->next = \$newNode;
//newNode will become new tail of the list
\$this->tail = \$newNode;
}
}

//minNode() will find out the minimum value node in the list
function minNode() {

print("List is empty <br>");
}
else {
//Initializing min with head node data

while(\$current != null){
//If current node's data is smaller than min
//Then, replace value of min with current node's data
if(\$min > \$current->data) {
\$min = \$current->data;
}
\$current = \$current->next;
}
print("Minimum value node in the list: ". \$min);
}
}

//maxNode() will find out the maximum value node in the list
function maxNode() {

print("List is empty <br>");
}
else {
//Initializing max with head node data

while(\$current != null){
//If current node's data is greater than max
//Then, replace value of max with current node's data
if(\$max < \$current->data) {
\$max = \$current->data;
}
\$current = \$current->next;
}
print("<br>Maximum value node in the list: ". \$max);
}
}
}

\$sList = new MinMax();

//Display the minimum value node in the list
\$sList->minNode();

//Display the maximum value node in the list
\$sList->maxNode();
?>
</body>
</html>
```

Output:

``` Minimum value node in the list: 1
Maximum value node in the list: 8
```

Next Topic#