Check if linked list is sorted (Iterative and Recursive)

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”.

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program to check Linked List is sorted
// in descending order or not
#include <bits/stdc++.h>
using namespace std;
  
/* Linked list node */
struct Node
{
    int data;
    struct Node* next;
};
  
// function to Check Linked List is 
// sorted in descending order or not
bool isSortedDesc(struct Node *head)
    if (head == NULL)
        return true;
  
    // Traverse the list till last node and return
    // false if a node is smaller than or equal
    // its next.
    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;
}
  
// Driver program to test above
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;
}

chevron_right


Output:

Yes

Time Complexity : O(N), where N is the length of linked list.

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.

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program to recursively check Linked List 
// is sorted in descending order or not
#include <bits/stdc++.h>
using namespace std;
  
/* Linked list node */
struct Node
{
    int data;
    struct Node* next;
};
  
// function to Check Linked List is 
// sorted in descending order or not
bool isSortedDesc(struct Node *head)
    // Base cases
    if (head == NULL || head->next == NULL)
        return true;
      
    // Check first two nodes and recursively
    // check remaining.   
    return (head->data > head->next->data && 
        isSortedDesc(head->next));
}
  
Node *newNode(int data)
{
   Node *temp = new Node;
   temp->next = NULL;
   temp->data = data;
}
  
// Driver program to test above
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;
}

chevron_right


Output:

Yes


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.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.