โ† Back to list

๐Ÿ” How I Verified If a Linked List Is Sorted Using a Simple Count-Based Approach

When working with linked lists, one common interview or coding challenge is:

Srikanthtalari ยท 2025-06-10 18:30 ยท 3 claps ยท 1.6 min read
#linked-list-program #sorted #ascending #descending #linear-time-complexity
Open on Medium โ†—
Wiki topics: ๐Ÿ’ป ยท Programming

๐Ÿ” How I Verified If a Linked List Is Sorted Using a Simple Count-Based Approach

When working with linked lists, one common interview or coding challenge is:

โ€œDetermine whether a singly linked list is sorted (ascending or descending).โ€

Most people use two flags (like ascending, descending) to track the trend. That works, but I found a cleaner and more elegant way โ€” and I want to share it with you.

๐Ÿง  The Core Idea

Instead of tracking booleans, we count how many steps are in the ascending or descending direction, and use that to determine if the list is sorted.

โœ… Java Implementation: Check if the List Is Sorted

class Node {
    int data;
    Node next;
}
class Solution {
    public static boolean isSorted(Node head) {
        int size = 0;
        int sortCount = 0;
        while (head.next != null) {
            if (head.data < head.next.data) {
                sortCount++;
                size++;
            } else if (head.data > head.next.data) {
                sortCount--;
                size++;
            }
            head = head.next;
        }
        return size == Math.abs(sortCount);
    }
}

๐Ÿงช How This Works

  • size counts the total number of comparisons (ignoring equal values).
  • sortCount increments for ascending pairs and decrements for descending.
  • If all comparisons go in the same direction, Math.abs(sortCount) == size.

โœ… Test Cases

Linked ListOutput1 โ†’ 2 โ†’ 3true3 โ†’ 2 โ†’ 1true2 โ†’ 2 โ†’ 2true1 โ†’ 3 โ†’ 2 โ†’ 4false

๐Ÿงญ Bonus: Detecting Ascending or Descending

public static String getSortOrder(Node head) {
    int size = 0;
    int sortCount = 0;
    while (head != null && head.next != null) {
        if (head.data < head.next.data) {
            sortCount++;
            size++;
        } else if (head.data > head.next.data) {
            sortCount--;
            size++;
        }
        head = head.next;
    }
    if (size == 0) return "all elements equal";
    if (sortCount == size) return "ascending";
    if (sortCount == -size) return "descending";
    return "unsorted";
}

โœ… Direction Test Cases

Linked ListOutput1 โ†’ 2 โ†’ 3"ascendingโ€5 โ†’ 4 โ†’ 2"descendingโ€3 โ†’ 3 โ†’ 3 โ†’ 3"all elements equalโ€1 โ†’ 3 โ†’ 2 โ†’ 4"unsortedโ€

๐ŸŽฏ Why This Approach Works So Well

  • One-pass (O(n)) time complexity
  • Handles duplicates naturally
  • Compact and readable
  • Doesnโ€™t require tracking two flags

๐Ÿ“Œ Final Thoughts

There are always multiple ways to solve a problem โ€” and while flag-based solutions are common, this count-based approach is just as valid, often cleaner, and fun to explain.

If youโ€™re preparing for interviews or like writing smart utility functions โ€” try this out. And if youโ€™re already using it โ€” youโ€™re ahead of the curve! ๐Ÿ˜‰


๋ฉ”ํƒ€๋ฐ์ดํ„ฐ
post_id
3f37a1e9481b
slug
how-i-verified-if-a-linked-list-is-sorted-using-a-simple-count-based-approach-3f37a1e9481b
url
https://medium.com/@srikanthtalari222/how-i-verified-if-a-linked-list-is-sorted-using-a-simple-count-based-approach-3f37a1e9481b
canonical_url
https://medium.com/@srikanthtalari222/how-i-verified-if-a-linked-list-is-sorted-using-a-simple-count-based-approach-3f37a1e9481b
author_url
https://medium.com/@srikanthtalari222
status
ok
fetched_at
2026-07-19 12:29:59