Related Articles
Check if a Linked List is Pairwise Sorted
• Difficulty Level : Basic
• Last Updated : 28 Jan, 2020

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.

Examples:

Input: List =  10 -> 15 -> 9 -> 9 -> 1 -> 5
Output: YES

Input: List = 10 -> 15 -> 8 -> 9 -> 10 -> 5
Output: NO

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

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:

## C#

Output:

YES

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.

My Personal Notes arrow_drop_up
Recommended Articles
Page :