Q:

Java program to find the maximum and minimum value node from a linked list

belongs to collection: Java Singly Linked List Programs

0

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 the minimum node is given below.

Algorithm

  • Create a class Node which has two attributes: data and next. Next is a pointer to the next node in the list.
  • Create another class MinMax which has two attributes: head and tail.
  • addNode() will add a new node to the list:
    • Create a new node.
    • It first checks, whether the head is equal to null which means the list is empty.
    • If the list is empty, both head and tail will point to a newly added node.
    • 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.

a. minNode() will display minimum value node:

  • Define a variable min and initialize it with head's data.
  • Node current will point to head.
  • Iterate through the list by comparing each node's data with min.
  • If min is greater than current's data then, min will hold current's data.
  • At the end of the list, variable min will hold minimum value node.
  • Display the min value.

a. maxNode() will display maximum value node:

  • Define a variable max and initialize it with head's data.
  • Node current will point to head.
  • Iterate through the list by comparing each node's data with max.
  • If max is less than current's data then, max will hold current's data.
  • At the end of the list, variable max will hold maximum value node.
  • Display the max value

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Program:

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;  
        }  
    }  
  
    //Represent the head and tail of the singly linked list  
    public Node head = null;  
    public Node tail = null;  
  
    //addNode() will add a new node to the list  
    public void addNode(int data) {  
        //Create a new node  
        Node newNode = new Node(data);  
  
        //Checks if the list is empty  
        if(head == null) {  
            //If list is empty, both head and tail will point to new node  
            head = newNode;  
            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() {  
        Node current = head;  
        int min;  
  
        if(head == null) {  
            System.out.println("List is empty");  
        }  
        else {  
            //Initializing min with head node data  
            min = head.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() {  
        Node current = head;  
        int max;  
  
        if(head == null) {  
            System.out.println("List is empty");  
        }  
        else {  
            //Initializing max with head node data  
            max = head.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();  
  
        //Adds data to the list  
        sList.addNode(5);  
        sList.addNode(8);  
        sList.addNode(1);  
        sList.addNode(6);  
  
        //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

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Java Program to insert a new node at the middle of... >>
<< Java program to determine whether a singly linked ...