Find smallest and largest elements in singly linked list
Last Updated :
04 Sep, 2024
Given a singly linked list of n nodes, the task is to find the smallest and largest element in linked list.
Examples:
Input: 15 -> 14 -> 13 -> 22 -> 17
Output: 13 22
Explanation: The minimum element in the linked list is 13, and the maximum element is 22.
Input: 20 -> 25 -> 23 -> 68 -> 54 -> 14 -> 45
Output: 14 68
Explanation: The minimum element in the linked list is 14, and the maximum element is 68.
Approach:
The idea is to traverse the linked list while the head is not equal to NULL
and initialize the maxElement
and minElement
variables to INT_MIN
and INT_MAX
, respectively.
After that, check if the current node's value is greater than the current maxElement
value; if so, update maxElement
with the node's value. Similarly, check if the current node's value is less than the current minElement
value; if so, update minElement
with the node's value. Move the head pointer to the next node, and continue this process until the head is equal to NULL
.
C++
// C++ Program to find smallest and largest
// elements in singly linked list.
#include <bits/stdc++.h>
using namespace std;
class Node {
public:
int data;
Node* next;
Node(int x) {
data = x;
next = nullptr;
}
};
// Function to find both the smallest and largest elements
pair<int, int> findMinMax(Node* head) {
// Initialize minElement and maxElement
int minElement = INT_MAX;
int maxElement = INT_MIN;
// Use a current node pointer to traverse the list
Node* curr = head;
// Traverse the linked list
while (curr != nullptr) {
// Update maxElement and minElement
if (curr->data > maxElement) {
maxElement = curr->data;
}
if (curr->data < minElement) {
minElement = curr->data;
}
// Move to the next node
curr = curr->next;
}
// Return the pair of minElement and maxElement
return make_pair(minElement, maxElement);
}
int main() {
// Create a hard-coded linked list:
// 15 -> 14 -> 13 -> 22 -> 17
Node* head = new Node(15);
head->next = new Node(14);
head->next->next = new Node(13);
head->next->next->next = new Node(22);
head->next->next->next->next = new Node(17);
pair<int, int> result = findMinMax(head);
cout << result.first << " " << result.second << endl;
return 0;
}
C
// C Program to find smallest and largest
// elements in singly linked list.
#include <stdio.h>
#include <limits.h>
struct Node {
int data;
struct Node* next;
};
struct Pair {
int minElement;
int maxElement;
};
// Function to find both the smallest and largest elements
struct Pair findMinMax(struct Node* head) {
// Initialize minElement and maxElement
struct Pair result;
result.minElement = INT_MAX;
result.maxElement = INT_MIN;
// Use a current node pointer to traverse the list
struct Node* curr = head;
// Traverse the linked list
while (curr != NULL) {
// Update maxElement and minElement
if (curr->data > result.maxElement) {
result.maxElement = curr->data;
}
if (curr->data < result.minElement) {
result.minElement = curr->data;
}
// Move to the next node
curr = curr->next;
}
// Return the pair of minElement and maxElement
return result;
}
struct Node* createNode(int x) {
struct Node* newNode =
(struct Node*)malloc(sizeof(struct Node));
newNode->data = x;
newNode->next = NULL;
return newNode;
}
int main() {
// Create a hard-coded linked list:
// 15 -> 14 -> 13 -> 22 -> 17
struct Node* head = createNode(15);
head->next = createNode(14);
head->next->next = createNode(13);
head->next->next->next = createNode(22);
head->next->next->next->next = createNode(17);
struct Pair result = findMinMax(head);
printf("%d %d\n", result.minElement, result.maxElement);
return 0;
}
Java
// Java Program to find smallest and largest
// elements in singly linked list.
class Node {
int data;
Node next;
Node(int x) {
data = x;
next = null;
}
}
class Pair {
public int minElement;
public int maxElement;
public Pair(int minElement, int maxElement) {
this.minElement = minElement;
this.maxElement = maxElement;
}
}
class GfG {
// Function to find both the smallest and largest elements
static Pair findMinMax(Node head) {
// Initialize minElement and maxElement
int minElement = Integer.MAX_VALUE;
int maxElement = Integer.MIN_VALUE;
// Use a current node pointer to traverse the list
Node curr = head;
// Traverse the linked list
while (curr != null) {
// Update maxElement and minElement
if (curr.data > maxElement) {
maxElement = curr.data;
}
if (curr.data < minElement) {
minElement = curr.data;
}
// Move to the next node
curr = curr.next;
}
// Return the pair of minElement and maxElement
return new Pair(minElement, maxElement);
}
public static void main(String[] args) {
// Create a hard-coded linked list:
// 15 -> 14 -> 13 -> 22 -> 17
Node head = new Node(15);
head.next = new Node(14);
head.next.next = new Node(13);
head.next.next.next = new Node(22);
head.next.next.next.next = new Node(17);
Pair result = findMinMax(head);
System.out.println(result.minElement +
" " + result.maxElement);
}
}
Python
# Python Program to find smallest and largest
# elements in singly linked list.
class Node:
def __init__(self, x):
self.data = x
self.next = None
# Function to find both the smallest and largest elements
def find_min_max(head):
# Initialize minElement and maxElement
min_element = float('inf')
max_element = float('-inf')
# Use a current node pointer to traverse the list
curr = head
# Traverse the linked list
while curr is not None:
# Update maxElement and minElement
if curr.data > max_element:
max_element = curr.data
if curr.data < min_element:
min_element = curr.data
# Move to the next node
curr = curr.next
# Return the pair of minElement and maxElement
return min_element, max_element
if __name__ == "__main__":
# Create a hard-coded linked list:
# 15 -> 14 -> 13 -> 22 -> 17
head = Node(15)
head.next = Node(14)
head.next.next = Node(13)
head.next.next.next = Node(22)
head.next.next.next.next = Node(17)
result = find_min_max(head)
print(result[0], result[1])
C#
// C# Program to find smallest and largest
// elements in singly linked list.
using System;
class Node {
public int data;
public Node next;
public Node(int x) {
data = x;
next = null;
}
}
class GfG {
// Function to find both the smallest and largest elements
static Tuple<int, int> findMinMax(Node head) {
// Initialize minElement and maxElement
int minElement = int.MaxValue;
int maxElement = int.MinValue;
// Use a current node pointer to traverse the list
Node curr = head;
// Traverse the linked list
while (curr != null) {
// Update maxElement and minElement
if (curr.data > maxElement) {
maxElement = curr.data;
}
if (curr.data < minElement) {
minElement = curr.data;
}
// Move to the next node
curr = curr.next;
}
// Return the pair of minElement and maxElement
return Tuple.Create(minElement, maxElement);
}
static void Main() {
// Create a hard-coded linked list:
// 15 -> 14 -> 13 -> 22 -> 17
Node head = new Node(15);
head.next = new Node(14);
head.next.next = new Node(13);
head.next.next.next = new Node(22);
head.next.next.next.next = new Node(17);
var result = findMinMax(head);
Console.WriteLine(result.Item1 + " " + result.Item2);
}
}
JavaScript
// JavaScript Program to find smallest and largest
// elements in singly linked list.
class Node {
constructor(x)
{
this.data = x;
this.next = null;
}
}
// Function to find both the smallest and largest elements
function findMinMax(head) {
// Initialize minElement and maxElement
let minElement = Number.POSITIVE_INFINITY;
let maxElement = Number.NEGATIVE_INFINITY;
// Traverse the linked list
for (let curr = head; curr !== null; curr = curr.next) {
// Update maxElement and minElement
if (curr.data > maxElement) {
maxElement = curr.data;
}
if (curr.data < minElement) {
minElement = curr.data;
}
}
return [ minElement, maxElement ];
}
// Create a hard-coded linked list: 15 -> 14 -> 13 -> 22 -> 17
let head = new Node(15);
head.next = new Node(14);
head.next.next = new Node(13);
head.next.next.next = new Node(22);
head.next.next.next.next = new Node(17);
let [minElement, maxElement] = findMinMax(head);
console.log(minElement, maxElement);
Time Complexity: O(n)
Auxiliary Space: O(1)
Similar Reads
Find the Second Largest Element in a Linked List Given a Linked list of integer data. The task is to write a program that efficiently finds the second largest element present in the Linked List. Examples: Input : List = 12 -> 35 -> 1 -> 10 -> 34 -> 1 Output : The second largest element is 34. Input : List = 10 -> 5 -> 10 Outpu
9 min read
find N largest elements from a Linked list Given a Linked list of integers, the task is to find N largest elements in the Linked List. Examples: Input: [4, 5, 1, 2, 9], N = 2Output: [9, 5] Input: [81, 52, 45, 10, 3, 2, 96], N = 3Output: [ 96, 81, 52] Method 1 : Brute ForceApproach: To solve the problem follow the below idea: The idea is to S
15+ min read
Second Smallest Element in a Linked List Given a Linked list of integer data. The task is to write a program that efficiently finds the second smallest element present in the Linked List. Examples: Input : List = 12 -> 35 -> 1 -> 10 -> 34 -> 1Output : The second smallest element is 10.Input : List = 10 -> 5 -> 10Output
15+ min read
Find the largest node in Doubly linked list Given a doubly-linked list, find the largest node in the doubly linked list. Examples: Input: 10->8->4->23->67->88 Largest node is: 88 Output: 88 Input : 34->2->78->18->120->39->7 Largest node is: 120 Output :120 Approach Used: Initialize the temp and max pointer to
10 min read
Find a peak element in Linked List Given a linked list of integers, the task is to find a peak element in the given linked list. An element is a peak, if it is greater than or equals to its neighbors. For boundary elements, consider only one neighbor.Examples: Input : List = {1 -> 6 -> 8 -> 4 -> 12} Output : 8Explanation:
8 min read