Write a method isSorted that returns true if the list is in sorted (nondecreasing) order and returns false otherwise. An empty list is considered to be sorted.

Respuesta :

Answer:

// This method is written in Java Programming Language

// Comments are used for explanatory purpose

// Method starts here

// Declare method isSorted

public boolean isSorted() {

// Create a listnode

ListNode lst = list;

// Check if list is null

if (list == null) {

return true;

}

// Iterate while list is not null

while (lst.next != null) {

// Get current list item

ListNode current = lst.next;

// Compare; if less than, return false

if (current.data < lst.data) {

return false;

}

// Assign current to lst

lst = current;

}

return true;

}