I want to implement a function that uses an efficient sorting algorithm (mergesort, quicksort), to sort a linked list, with the list being the only parameter. It needs to be consistent with the following function declaration:
void list_sort( list_t* list );
I have looked up many mergesort codes for linked lists, but all of them usually have a head as the parameter, not a list. I am really stuck as to how to solve this. I was thinking perhaps copying all the elements into an array, sorting the array, then copying elements back into the list but was told I cannot do that. If anyone can help, would really appreciate it.
void list_sort( list_t* list ){
//if 0 or 1 element list
if (list->head==NULL || list->head->next==NULL) {
return;
}
//stuck on what to do after this point
//I know I need to split the list into two sublists, then recursively call list_sort again
//then finally merge the two sublists
}
**EDIT: here is list_t
// List element: a list is a chain of these
typedef struct element
{
int val;
struct element* next;
} element_t;
// List header - keep track of the first and last list elements
typedef struct list
{
element_t* head;
element_t* tail;
} list_t;