Complete the isSorted method of the below LinkedList class such that it determin
ID: 3862558 • Letter: C
Question
Complete the isSorted method of the below LinkedList class such that it determines whether the list is sorted (ascending or descending based on the input argument), i.e., it returns true if the numbers in the list are in strictly increasing order (no duplicates allowed) when ascending is true and it returns true if the numbers are in strictly decreasing order when ascending is false. The function shoud return false otherwise. (Java)
public class LinkedList private Node first if list is empty, then first m* null private class Node public int item; public Node next. public boolean issorted (boolean ascending) TODO recursive helper function private boolean issorted (Node n, boolean ascending)Explanation / Answer
public class LinkedList
{
private Node first;
private class Node
{
public int item;
public Node next;
Node()
{
item = 0;
next = null;
}
Node(int n)
{
item = n;
next = null;
}
Node(int n, Node p)
{
item = n;
next = p;
}
};
public boolean isSorted(ArrayList<Integer> list)
{
boolean isSorted = true;
for (int i = 1; i < list.size(); i++)
{
if (list.get(i-1) >= (list.get(i)) )
{
isSorted = true;
}
else
{
return false;
}
}
return isSorted;
}
public boolean issorted(ArrayList<Integer> list)
{
boolean issorted =true;
Collections.sort(list, Collections.reverseOrder());
for (int i = 1; i < list.size(); i++)
{
if(list.get(i)>=list.get(i-1))
{
issorted =true;
}
else
return false;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.