๐ 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:
๐ 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
sizecounts the total number of comparisons (ignoring equal values).sortCountincrements 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