JAVA isSorted returns whether the LinkedList is sorted in

JAVA  

/**
   * isSorted returns whether the LinkedList is sorted in increasing order.
   *
   * Examples:
   * LinkedList : 2 --> 3 --> null ==> return true
   * LinkedList : 1 --> -3 --> null ==> return false
   * LinkedList : -2 --> 3 --> -2 --> null ==> return false
   */
   public boolean isSorted() {
       return false;
   }

Solution

Hi, Please find my implementation.

Please let me know in case of any issue.


I am assuming that \'list\' is object of LinkedList.

/**
* isSorted returns whether the LinkedList is sorted in increasing order.
*
* Examples:
* LinkedList : 2 --> 3 --> null ==> return true
* LinkedList : 1 --> -3 --> null ==> return false
* LinkedList : -2 --> 3 --> -2 --> null ==> return false
*/
public boolean isSorted() {

   if(list == null || list.size() == 1)
       return true;

   for(int i = 0; i<list.size()-1; i++){
       // if list is in order
       if(list.get(i) > list.get(i+1))
           return false;
   }


return true;
}

JAVA /** * isSorted returns whether the LinkedList is sorted in increasing order. * * Examples: * LinkedList : 2 --> 3 --> null ==> return true * Linke

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site