Checking if List is Sorted
Procedure to check if a linked list is sorted or not.
Procedure :
int isSorted(struct node *ptr) {
// If only one node is present, it is sorted
if (ptr -> next == NULL) {
return 1;
}
else {
// Check until last node is reached
while (ptr -> next != NULL) {
// If data of previous node is greater
if (ptr -> data > ptr -> next -> data) {
return 0;
}
ptr = ptr -> next;
}
return 1;
}
}Last updated