Given a Linked List, task is to check whether the Linked List is sorted in Descending order or not?
Examples :
Input : 8 -> 7 -> 5 -> 2 -> 1
Output : Yes
Explanation :
In given linked list, starting from head,
8 > 7 > 5 > 2 > 1. So, it is sorted in reverse order
Input : 24 -> 12 -> 9 -> 11 -> 8 -> 2
Output : No
Iterative Approach: Traverse the linked list from head to end. For every newly encountered element, check node -> data > node -> next -> data. If True, do same for each node else return 0 and Print “No”.
Implementation:
C++
#include <bits/stdc++.h>
using namespace std;
struct Node
{
int data;
struct Node* next;
};
bool isSortedDesc( struct Node *head)
{
if (head == NULL)
return true ;
for (Node *t=head; t->next != NULL; t=t->next)
if (t->data <= t->next->data)
return false ;
return true ;
}
Node *newNode( int data)
{
Node *temp = new Node;
temp->next = NULL;
temp->data = data;
}
int main()
{
struct Node *head = newNode(7);
head->next = newNode(5);
head->next->next = newNode(4);
head->next->next->next = newNode(3);
isSortedDesc(head) ? cout << "Yes" :
cout << "No" ;
return 0;
}
|
Java
class GFG
{
static class Node
{
int data;
Node next;
};
static boolean isSortedDesc(Node head)
{
if (head == null )
return true ;
for (Node t = head; t.next != null ; t = t.next)
if (t.data <= t.next.data)
return false ;
return true ;
}
static Node newNode( int data)
{
Node temp = new Node();
temp.next = null ;
temp.data = data;
return temp;
}
public static void main(String[] args)
{
Node head = newNode( 7 );
head.next = newNode( 5 );
head.next.next = newNode( 4 );
head.next.next.next = newNode( 3 );
if (isSortedDesc(head))
System.out.println( "Yes" );
else
System.out.println( "No" );
}
}
|
Python3
class Node:
def __init__( self , data):
self .data = data;
self . next = next ;
def isSortedDesc(head):
if (head = = None ):
return True ;
while (head. next ! = None ):
t = head;
if (t.data < = t. next .data):
return False ;
head = head. next
return True ;
def newNode(data):
temp = Node( 0 );
temp. next = None ;
temp.data = data;
return temp;
if __name__ = = '__main__' :
head = newNode( 7 );
head. next = newNode( 5 );
head. next . next = newNode( 4 );
head. next . next . next = newNode( 3 );
if (isSortedDesc(head)):
print ( "Yes" );
else :
print ( "No" );
|
C#
using System;
class GFG
{
public class Node
{
public int data;
public Node next;
};
static bool isSortedDesc(Node head)
{
if (head == null )
return true ;
for (Node t = head;
t.next != null ; t = t.next)
if (t.data <= t.next.data)
return false ;
return true ;
}
static Node newNode( int data)
{
Node temp = new Node();
temp.next = null ;
temp.data = data;
return temp;
}
public static void Main(String[] args)
{
Node head = newNode(7);
head.next = newNode(5);
head.next.next = newNode(4);
head.next.next.next = newNode(3);
if (isSortedDesc(head))
Console.WriteLine( "Yes" );
else
Console.WriteLine( "No" );
}
}
|
Javascript
<script>
class Node {
constructor() {
this .data = 0;
this .next = null ;
}
}
function isSortedDesc(head) {
if (head == null || head.next == null ) return true ;
return head.data > head.next.data && isSortedDesc(head.next);
}
function newNode(data) {
var temp = new Node();
temp.next = null ;
temp.data = data;
return temp;
}
var head = newNode(7);
head.next = newNode(5);
head.next.next = newNode(4);
head.next.next.next = newNode(3);
if (isSortedDesc(head) == true ) document.write( "Yes" );
else document.write( "No" );
</script>
|
Time Complexity : O(N), where N is the length of linked list.
Auxiliary Space: O(1) because it is using constant variables
Recursive Approach: Check Recursively that node -> data > node -> next -> data, If not, return 0 that is our terminated condition to come out from recursion else Call Check_List Function Recursively for next node.
Implementation:
C++
#include <bits/stdc++.h>
using namespace std;
struct Node
{
int data;
struct Node* next;
};
bool isSortedDesc( struct Node *head)
{
if (head == NULL || head->next == NULL)
return true ;
return (head->data > head->next->data &&
isSortedDesc(head->next));
}
Node *newNode( int data)
{
Node *temp = new Node;
temp->next = NULL;
temp->data = data;
}
int main()
{
struct Node *head = newNode(7);
head->next = newNode(5);
head->next->next = newNode(4);
head->next->next->next = newNode(3);
isSortedDesc(head) ? cout << "Yes" :
cout << "No" ;
return 0;
}
|
Java
class GfG {
static class Node
{
int data;
Node next;
}
static boolean isSortedDesc(Node head)
{
if (head == null || head.next == null )
return true ;
return (head.data > head.next.data && isSortedDesc(head.next));
}
static Node newNode( int data)
{
Node temp = new Node();
temp.next = null ;
temp.data = data;
return temp;
}
public static void main(String[] args)
{
Node head = newNode( 7 );
head.next = newNode( 5 );
head.next.next = newNode( 4 );
head.next.next.next = newNode( 3 );
if (isSortedDesc(head) == true )
System.out.println( "Yes" );
else
System.out.println( "No" );
}
}
|
Python3
class Node:
def __init__( self , data):
self .data = data
self . next = None
def isSortedDesc(head):
if (head = = None or head. next = = None ):
return True
return (head.data > head. next .data and
isSortedDesc(head. next ))
def newNode(data):
temp = Node(data)
return temp
if __name__ = = "__main__" :
head = newNode( 7 )
head. next = newNode( 5 )
head. next . next = newNode( 4 )
head. next . next . next = newNode( 3 )
if isSortedDesc(head):
print ( "Yes" )
else :
print ( "No" )
|
C#
using System;
class GfG
{
public class Node
{
public int data;
public Node next;
}
static bool isSortedDesc(Node head)
{
if (head == null || head.next == null )
return true ;
return (head.data > head.next.data &&
isSortedDesc(head.next));
}
static Node newNode( int data)
{
Node temp = new Node();
temp.next = null ;
temp.data = data;
return temp;
}
public static void Main(String[] args)
{
Node head = newNode(7);
head.next = newNode(5);
head.next.next = newNode(4);
head.next.next.next = newNode(3);
if (isSortedDesc(head) == true )
Console.WriteLine( "Yes" );
else
Console.WriteLine( "No" );
}
}
|
Javascript
<script>
class Node
{
constructor(data) {
this .next = null ;
this .data = data;
}
}
function isSortedDesc(head)
{
if (head == null || head.next == null )
return true ;
return (head.data > head.next.data &&
isSortedDesc(head.next));
}
function newNode(data)
{
let temp = new Node(data);
return temp;
}
let head = newNode(7);
head.next = newNode(5);
head.next.next = newNode(4);
head.next.next.next = newNode(3);
if (isSortedDesc(head) == true )
document.write( "Yes" );
else
document.write( "No" );
</script>
|
Complexities Analysis:
- Time complexity: O(N) where N is no of nodes in linked list.
- Auxiliary Space: O(N)