374. Check if Linked List is Sorted

MediumLinked ListLinked ListTraversal

Given a linked list, determine whether it is sorted. Return "ascending" if it is non-decreasing, "descending" if it is non-increasing, "both" if it is constant (or has 0/1 nodes), and "neither" otherwise.

Input: An array of node values.

Output: Quoted string — one of ascending/descending/both/neither.

Examples

Example 1
Input: [1,2,3,4]
Output: "ascending"
Explanation: Non-decreasing.
Example 2
Input: [4,3,2,1]
Output: "descending"
Explanation: Non-increasing.
Example 3
Input: [5,5,5]
Output: "both"
Explanation: Constant counts as both.

Constraints

Asked by

AmazonMicrosoftGoogleAdobeFlipkart
Solve this problem in the editor →