Given a linked list. The task is to check if the linked list is pairwise sorted or not.
A Linked List is considered pairwise sorted if each successive pair of numbers is in sorted (non-decreasing) order. In the case of odd number of nodes, the last node is ignored and the result is based on remaining even number of nodes.
Input: List = 10 -> 15 -> 9 -> 9 -> 1 -> 5 Output: YES Input: List = 10 -> 15 -> 8 -> 9 -> 10 -> 5 Output: NO
Approach: The idea is to traverse the linked list from left to right. Compare nodes pairwise, if any pair violates property, return false. If no pair violates property, return True.
Below is the implementation of above approach:
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.
- Merge K sorted Doubly Linked List in Sorted Order
- Check if linked list is sorted (Iterative and Recursive)
- Sort a linked list that is sorted alternating ascending and descending orders?
- Count triplets in a sorted doubly linked list whose product is equal to a given value x
- Merge two unsorted linked lists to get a sorted list
- Maximum sum of pairwise product in an array with negative allowed
- Maximize sum of pairwise products generated from the given Arrays
- XOR of all possible pairwise sum from two given Arrays
- Sort a nearly sorted (or K sorted) array
- Maximum number of partitions that can be sorted individually to make sorted
- Intersection of two Sorted Linked Lists
- Python | Returning index of a sorted list
- Find the Kth pair in ordered list of all possible sorted pairs of the Array
- Program to check if an array is sorted or not (Iterative and Recursive)
- Check if reversing a sub array make the array sorted
- Check whether a given array is a k sorted array or not
- Check if a grid can become row-wise and column-wise sorted after adjacent swaps
- Check if given array is almost sorted (elements are at-most one position away)
- Check if a queue can be sorted into another queue using a stack
- Check if array can be sorted with one swap
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.