java - How do I know if my LinkedList is already ordered? -
this question has answer here:
- how determine if list sorted in java? 11 answers
private static linkedlist<integer> melhormoto1 = new linkedlist<>(); i know can create new linkedlist, use newlinkedlist = melhormoto1;and collections.sort(newlinkedlist); , newlinkedlist.equals(melhormoto1 ); i'm working recursive function, newlinkedlist = melhormoto1; slow attribution. can check if linkedlist ordered method or something?
in java 8, can use comparators.isinorder(iterable) achieve list (or other ordered collection).
prior java 7, yourself: check list sorted in ascending order, iterate through , check current element cur greater or equal previous element prev each adjacent pair of elements.
change less or equal descending order. requires size() - 1 total checks.
Comments
Post a Comment