Given a singly linked list of nodes. Find the smallest and largest elements in linked list divisible by a given number .
Input : List = 15 -> 14 -> 13 -> 22 -> 50
K = 5
Maximum element in linked list divisible by K: 50
Minimum element in linked list divisible by K: 5
Input : List = 10 -> 14 -> 13 -> 22 -> 100
K = 10
Maximum element in linked list divisible by K: 100
Minimum element in linked list divisible by K: 10
- The idea is to traverse the linked list to the end and initialize the max and min variable to INT_MIN and INT_MAX respectively.
- After that check a condition that if the max value is less then the current node’s value and divisible by K then current node’s value is assigned to max.
- Similarly check if the current node’s value is less than min value and divisible by k then the current node’s value is assigned to min. Repeat above two steps until the end of the list is reached.
Below is the implementation of the above approach:
Max Element : 50 Min Element : 5
Time complexity : O(n)
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Minimum and Maximum element of an array which is divisible by a given number k
- Sum of all nodes in a doubly linked list divisible by a given number K
- Product of all nodes in a doubly linked list divisible by a given number K
- Minimum and Maximum Prime Numbers of a Singly Linked List
- Find minimum and maximum elements in singly Circular Linked List
- Create new linked list from two given linked list with greater element at each node
- Create a linked list from two linked lists by choosing max element at each position
- Construct a Maximum Sum Linked List out of two Sorted Linked Lists having some Common nodes
- Sum and Product of the nodes of a Singly Linked List which are divisible by K
- Delete all nodes from the doubly linked list which are divisible by K
- Sum and Product of the nodes of a Circular Singly Linked List which are divisible by K
- Sum of (maximum element - minimum element) for all the subsets of an array.
- Move first element to end of a given Linked List
- Majority element in a linked list
- Second Smallest Element in a Linked List
- Next greater element in the Linked List
- Move last element to front of a given Linked List
- Move last element to front of a given Linked List | Set 2
- Find the Second Largest Element in a Linked List
- Rearrange a linked list in to alternate first and last element
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.