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:
GeeksforGeeks has prepared a complete interview preparation course with premium videos, theory, practice problems, TA support and many more features. Please refer Placement 100 for details
- Check if linked list is sorted (Iterative and Recursive)
- Pairwise swap elements of a given linked list
- Pairwise swap elements of a given linked list by changing links
- Pairwise swap adjacent nodes of a linked list by changing pointers | Set 2
- Merge K sorted Doubly Linked List in Sorted Order
- Insert value in sorted way in a sorted doubly linked list
- Construct a Maximum Sum Linked List out of two Sorted Linked Lists having some Common nodes
- Given a linked list which is sorted, how will you insert in sorted way
- Sorted Linked List to Balanced BST
- Sorted insert for circular linked list
- Merge two sorted linked list without duplicates
- Remove duplicates from a sorted linked list
- Finding Median in a Sorted Linked List
- Sort a k sorted doubly linked list
- Create a sorted linked list from the given Binary Tree
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. 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.