
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
Find smallest and largest elements in singly linked list in C++
In this problem, we are given a singly linked list. Our task is to find the smallest and largest elements in single linked list.
Let’s take an example to understand the problem,
Input
linked List : 5 -> 2 -> 7 -> 3 ->9 -> 1 -> 4
Output
Smallest element = 1 Largest element = 9
Solution Approach
A simple solution to the problem is using by traversing the linked list node by node. Prior to this, we will initialise maxElement and minElement to the value of the first element i.e. head -> data. Then we will traverse the linked list element by element. And then compare the value of the current node with maxElement and store the greater value in maxElement variable. Perform the same to store smaller values in minElement. When the traversal is done print both the values.
Program to illustrate the working of our solution,
Example
#include <bits/stdc++.h> using namespace std; struct Node { int data; struct Node* next; }; void printLargestSmallestLinkedList(struct Node* head) { int maxElement = INT_MIN; int minElement = INT_MAX; while (head != NULL) { if (minElement > head->data) minElement = head->data; if (maxElement < head->data) maxElement = head->data; head = head->next; } cout<<"Smallest element in the linked list is : "<<minElement<<endl; cout<<"Largest element in the linked list is : "<<maxElement<<endl; } void push(struct Node** head, int data) { struct Node* newNode = (struct Node*)malloc(sizeof(struct Node)); newNode->data = data; newNode->next = (*head); (*head) = newNode; } int main() { struct Node* head = NULL; push(&head, 5); push(&head, 2); push(&head, 7); push(&head, 3); push(&head, 9); push(&head, 1); push(&head, 4); printLargestSmallestLinkedList(head); return 0; }
Output
Smallest element in the linked list is : 1 Largest element in the linked list is : 9
- Related Articles
- Find minimum and maximum elements in singly Circular Linked List in C++
- Remove elements from singly linked list in JavaScript
- Difference between Singly linked list and Doubly linked list in Java
- Python program to find Largest, Smallest, Second Largest, and Second Smallest in a List?
- Convert singly linked list into circular linked list in C++
- Convert singly linked list into XOR linked list in C++
- Find middle of singly linked list Recursively in C++
- C# program to find Largest, Smallest, Second Largest, Second Smallest in a List
- Singly Linked List as Circular in Javascript
- Find the common nodes in two singly linked list in C++
- Binary Search on Singly Linked List in C++
- Alternate Odd and Even Nodes in a Singly Linked List in C++
- Find the largest node in Doubly linked list in C++
- C++ Program to Implement Singly Linked List
- Program to find the middle node of a singly linked list in Python

Advertisements