Given a Linked List, task is to check whether the Linked List is sorted in Descending order or not?
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”.
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.
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
- wmemset() in C/C++ with Examples
- wcsspn() function in C/C++ with Examples
- #pragma Directive in C/C++
- errno constant in C++
- generate_canonical Function template in C++
- wcstoimax() and wcstoumax() function in C/C++
- mbsrtowcs() function in C/C++
- Finding the Parity of a number Efficiently
- Reference to a pointer in C++ with examples and applications
- Amadeus Labs Bangalore Interview experience