Check if a Linked List is Pairwise Sorted
Last Updated :
22 Dec, 2022
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 the 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
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++
#include <iostream>
using namespace std;
struct Node {
int data;
struct Node* next;
};
bool isPairWiseSorted( struct Node* head)
{
bool flag = true ;
struct Node* temp = head;
while (temp != NULL && temp->next != NULL) {
if (temp->data > temp->next->data) {
flag = false ;
break ;
}
temp = temp->next->next;
}
return flag;
}
void push( struct Node** head_ref, int new_data)
{
struct Node* new_node = ( struct Node*) malloc ( sizeof ( struct Node));
new_node->data = new_data;
new_node->next = (*head_ref);
(*head_ref) = new_node;
}
int main()
{
struct Node* start = NULL;
push(&start, 5);
push(&start, 1);
push(&start, 9);
push(&start, 9);
push(&start, 15);
push(&start, 10);
if (isPairWiseSorted(start))
cout << "YES" ;
else
cout << "NO" ;
return 0;
}
|
Java
class GFG
{
static class Node
{
int data;
Node next;
};
static Node start;
static boolean isPairWiseSorted(Node head)
{
boolean flag = true ;
Node temp = head;
while (temp != null && temp.next != null )
{
if (temp.data > temp.next.data)
{
flag = false ;
break ;
}
temp = temp.next.next;
}
return flag;
}
static void push(Node head_ref, int new_data)
{
Node new_node = new Node();
new_node.data = new_data;
new_node.next = head_ref;
head_ref = new_node;
start = head_ref;
}
public static void main(String[] args)
{
start = null ;
push(start, 5 );
push(start, 1 );
push(start, 9 );
push(start, 9 );
push(start, 15 );
push(start, 10 );
if (isPairWiseSorted(start))
System.out.println( "YES" );
else
System.out.println( "NO" );
}
}
|
Python
class Node:
def __init__( self , data):
self .data = data
self . next = None
start = None
def isPairWiseSorted(head) :
flag = True
temp = head
while (temp ! = None and temp. next ! = None ) :
if (temp.data > temp. next .data) :
flag = False
break
temp = temp. next . next
return flag
def push(head_ref, new_data) :
global start
new_node = Node( 0 )
new_node.data = new_data
new_node. next = head_ref
head_ref = new_node
start = head_ref
start = None
push(start, 5 )
push(start, 1 )
push(start, 9 )
push(start, 9 )
push(start, 15 )
push(start, 10 )
if (isPairWiseSorted(start)) :
print ( "YES" )
else :
print ( "NO" )
|
C#
using System;
class GFG
{
public class Node
{
public int data;
public Node next;
};
static Node start;
static Boolean isPairWiseSorted(Node head)
{
Boolean flag = true ;
Node temp = head;
while (temp != null && temp.next != null )
{
if (temp.data > temp.next.data)
{
flag = false ;
break ;
}
temp = temp.next.next;
}
return flag;
}
static void push(Node head_ref, int new_data)
{
Node new_node = new Node();
new_node.data = new_data;
new_node.next = head_ref;
head_ref = new_node;
start = head_ref;
}
public static void Main(String[] args)
{
start = null ;
push(start, 5);
push(start, 1);
push(start, 9);
push(start, 9);
push(start, 15);
push(start, 10);
if (isPairWiseSorted(start))
Console.WriteLine( "YES" );
else
Console.WriteLine( "NO" );
}
}
|
Javascript
<script>
class Node {
constructor() {
this .data = 0;
this .next = null ;
}
}
var start = null ;
function isPairWiseSorted(head) {
var flag = true ;
var temp = head;
while (temp != null && temp.next != null ) {
if (temp.data > temp.next.data) {
flag = false ;
break ;
}
temp = temp.next.next;
}
return flag;
}
function push(head_ref, new_data) {
var new_node = new Node();
new_node.data = new_data;
new_node.next = head_ref;
head_ref = new_node;
start = head_ref;
}
start = null ;
push(start, 5);
push(start, 1);
push(start, 9);
push(start, 9);
push(start, 15);
push(start, 10);
if (isPairWiseSorted(start)) document.write( "YES" );
else document.write( "NO" );
</script>
|
Complexity Analysis:
- Time Complexity: O(n)
- Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...