I was trying to figure out a way to sort a linked list in ascending or descending order, but because a linked list is accessed only sequentially, I don't know how to do it, let me explain it better, this is a code sketch of navigating a linked list in C (for the sake of this question, it's not important what list implementation do you use, it's only about the algorithm design of this specific problem):
Item* tmp = i; // i is a list, and tmp is a pointer to the list,
while(!listisempty(tmp)) {
// do something
tmp = tmp->next;
}
this is a possible way to navigate the linked list, I can only access the current element and the next element, i can't access directly the last element to sort the list efficiently,
Item* tmp = i; // i is a list, and tmp is a pointer to the list,
while(!listisempty(tmp)) {
if (current > next) {
swap(current, next);
}
tmp = tmp->next;
}
but this idea is not efficient, because i have to navigate the linked list multiple times in order to sort the list.
is there a way of starting with an arbitrary element in the list in order to sort it?
[I can try the gnome sort (i.e stupid sort), but it's not efficient and therefore i've ignored it (plus, the list can be long and it may require a lot of steps)]