Find smallest and largest elements in singly linked list
Given a singly linked list of n nodes and find the smallest and largest elements in linked list.
Examples:
Input : 15 14 13 22 17
Output : Linked list are:
17 -> 22 -> 13 -> 14 -> 15 -> NULL
Maximum element in linked list: 22
Minimum element in linked list: 13
Input : 20 25 23 68 54 13 45
Output : Linked list are:
45 -> 13 -> 54 -> 68 -> 23 -> 25 -> 20 -> NULL
Maximum element in linked list: 68
Minimum element in linked list: 13
The idea is to traverse the linked list while head not equal to NULL and initialise the max and min variable to INT_MIN and INT_MAX respectively. After that check a condition that if max value is less then head value is assigned to max or min value is greater then head value is assigned to min otherwise head point to next node. Continue this process until head not equal to NULL.
C++
#include <bits/stdc++.h>
using namespace std;
struct Node {
int data;
struct Node* next;
};
int largestElement( struct Node* head)
{
int max = INT_MIN;
while (head != NULL) {
if (max < head->data)
max = head->data;
head = head->next;
}
return max;
}
int smallestElement( struct Node* head)
{
int min = INT_MAX;
while (head != NULL) {
if (min > head->data)
min = head->data;
head = head->next;
}
return min;
}
void push( struct Node** head, int data)
{
struct Node* newNode =
( struct Node*) malloc ( sizeof ( struct Node));
newNode->data = data;
newNode->next = (*head);
(*head) = newNode;
}
void printList( struct Node* head)
{
while (head != NULL) {
printf ( "%d -> " , head->data);
head = head->next;
}
cout << "NULL" << endl;
}
int main()
{
struct Node* head = NULL;
push(&head, 15);
push(&head, 14);
push(&head, 13);
push(&head, 22);
push(&head, 17);
cout << "Linked list is : " << endl;
printList(head);
cout << "Maximum element in linked list:" ;
cout << largestElement(head) << endl;
cout << "Minimum element in linked list:" ;
cout << smallestElement(head) << endl;
return 0;
}
|
Java
class GfG
{
static class Node
{
int data;
Node next;
}
static Node head = null ;
static int largestElement(Node head)
{
int max = Integer.MIN_VALUE;
while (head != null )
{
if (max < head.data)
max = head.data;
head = head.next;
}
return max;
}
static int smallestElement(Node head)
{
int min = Integer.MAX_VALUE;
while (head != null )
{
if (min > head.data)
min = head.data;
head = head.next;
}
return min;
}
static void push( int data)
{
Node newNode = new Node();
newNode.data = data;
newNode.next = (head);
(head) = newNode;
}
static void printList(Node head)
{
while (head != null ) {
System.out.print(head.data + " -> " );
head = head.next;
}
System.out.println( "NULL" );
}
public static void main(String[] args)
{
push( 15 );
push( 14 );
push( 13 );
push( 22 );
push( 17 );
System.out.println( "Linked list is : " ) ;
printList(head);
System.out.print( "Maximum element in linked list: " );
System.out.println(largestElement(head));
System.out.print( "Minimum element in linked list: " );
System.out.print(smallestElement(head));
}
}
|
Python
class Node:
def __init__( self ):
self .data = None
self . next = None
head = None
def largestElement(head):
max = - 32767
while (head ! = None ):
if ( max < head.data) :
max = head.data
head = head. next
return max
def smallestElement(head):
min = 32767
while (head ! = None ) :
if ( min > head.data) :
min = head.data
head = head. next
return min
def push( data) :
global head
newNode = Node()
newNode.data = data
newNode. next = (head)
(head) = newNode
def printList( head) :
while (head ! = None ) :
print (head.data ,end = " -> " )
head = head. next
print ( "None" )
push( 15 )
push( 14 )
push( 13 )
push( 22 )
push( 17 )
print ( "Linked list is : " )
printList(head)
print ( "Maximum element in linked list: " ,end = "")
print (largestElement(head))
print ( "Minimum element in linked list: " ,end = "")
print (smallestElement(head),end = "")
|
C#
using System;
class GfG
{
class Node
{
public int data;
public Node next;
}
static Node head = null ;
static int largestElement(Node head)
{
int max = int .MinValue;
while (head != null )
{
if (max < head.data)
max = head.data;
head = head.next;
}
return max;
}
static int smallestElement(Node head)
{
int min = int .MaxValue;
while (head != null )
{
if (min > head.data)
min = head.data;
head = head.next;
}
return min;
}
static void push( int data)
{
Node newNode = new Node();
newNode.data = data;
newNode.next = (head);
(head) = newNode;
}
static void printList(Node head)
{
while (head != null )
{
Console.Write(head.data + " -> " );
head = head.next;
}
Console.WriteLine( "NULL" );
}
public static void Main()
{
push( 15);
push( 14);
push( 13);
push( 22);
push( 17);
Console.WriteLine( "Linked list is : " ) ;
printList(head);
Console.Write( "Maximum element in linked list: " );
Console.WriteLine(largestElement(head));
Console.Write( "Minimum element in linked list: " );
Console.Write(smallestElement(head));
}
}
|
Javascript
<script>
class Node {
constructor(val) {
this .data = val;
this .next = null ;
}
}
var head = null ;
function largestElement(head) {
var max = Number.MIN_VALUE;
while (head != null ) {
if (max < head.data)
max = head.data;
head = head.next;
}
return max;
}
function smallestElement(head) {
var min = Number.MAX_VALUE;
while (head != null ) {
if (min > head.data)
min = head.data;
head = head.next;
}
return min;
}
function push(data) {
var newNode = new Node();
newNode.data = data;
newNode.next = (head);
(head) = newNode;
}
function printList(head) {
while (head != null ) {
document.write(head.data + " -> " );
head = head.next;
}
document.write( "NULL" );
}
push(15);
push(14);
push(13);
push(22);
push(17);
document.write( "Linked list is : <br/>" );
printList(head);
document.write( "<br/>Maximum element in linked list: " );
document.write(largestElement(head));
document.write( "<br/>Minimum element in linked list: " );
document.write(smallestElement(head));
</script>
|
Output
Linked list is :
17 -> 22 -> 13 -> 14 -> 15 -> NULL
Maximum element in linked list:22
Minimum element in linked list:13
Time Complexity: O(n)
Auxiliary Space: O(1)
Last Updated :
10 Jan, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...