# Find the smallest and largest elements in a singly linked list

### Introduction

The linked list is one of the most important concepts and data structures to learn while preparing for interviews. Having a good grasp of Linked Lists can be a huge plus point in a coding interview.

### Problem Statement

In this question, we are given a singly linked list. We have to find the smallest and the largest element of the linked list.

### Problem Statement Understanding

Let’s try to understand the problem with help of example. In this list, the smallest element is 2 and the largest element is 9. So, the final output will be:

• Maximum Element: 9
• Minimum Element: 2

Input : Output : Maximum Element: 9, Minimum Element: 2.

Explanation: As the maximum element is 9 and the minimum element is 2, we are printing 9 and 2.

This question is not a tricky one. We just have to make use of simple list traversal to solve this problem. Let us have a glance at the approach.

### Approach

The approach is going to be simple.

• We will create two variables min and max.

• min will be initialized with INT_MAX and max will be initialized with INT_MIN.

• We are using INT_MAX because all the integers are lesser than it, and INT_MIN because all the integers are greater than it. With the help of these, finding the minimum and maximum becomes easy.

• Now, we will traverse through the given list, and for every node, we will have two checks.
1) If the data of the current node is less than the data of min, we will store the current node’s data in min.
2) Else, If the data of the current node is greater than max, we will store the current node’s data in max.

• After reaching the end of the list, min and max will contain the smallest and the largest element, respectively.

### Algorithm (Largest element)

• Create a variable max and initialize it with INT_MIN.
• Traverse through the list and for every node, compare its data with max.
• If the current node’s data is greater than max, then store the value of the current node’s data in max.
• In the end, max will contain the largest element of the list.

### Algorithm (Smallest element)

• Create a variable min and initialize it with INT_MAX.
• Traverse through the list and for every node, compare its data with min.
• If the current node’s data is lesser than min, then store the value of the current node’s data in min.
• In the end, min will contain the largest element of the list.

### Dry Run

Finding Maximum Element Finding Minimum Element ### Code Implementation

```#include

using namespace std;

struct Node {
int data;
struct Node* next;
};

{

int max = INT_MIN;

}
return max;
}

{

int min = INT_MAX;

}
return min;
}

void push(struct Node** head, int data)
{

struct Node* newNode =
(struct Node*)malloc(sizeof(struct Node));

newNode->data = data;

}

{
}
cout << "NULL" << endl;
}

int main()
{

cout << "Linked list is: " << endl;

cout << "Maximum element in linked list: ";

cout << "Minimum element in linked list: ";

return 0;
}
```
```public class PrepBytes
{

static class Node
{
int data;
Node next;
}

{

int max = Integer.MIN_VALUE;

{

}
return max;
}

{

int min = Integer.MAX_VALUE;

{

}
return min;
}

static void push(int data)
{

Node newNode = new Node();

newNode.data = data;

}

{
}
System.out.println("NULL");
}

public static void main(String[] args)
{
push( 9);
push( 7);
push( 2);
push( 5);
System.out.print("Maximum element in linked list: ");
System.out.print("Minimum element in linked list: ");
}
}
```