Hi, I'm trying to implement a recursive sorting of a singly linked list, so here it goes>
Algorithm:
1) find largest value in the list
2) put it on Head
3) start function again, but without that node
Node* sort_ll(Node* head) {
if(head == NULL)
return head; //should be base case
else
{
int max = 0;
Node* tmp = head;
Node* to_move = head;
while(tmp != NULL) //find max value and store it into to_move
{
if(tmp->data > max) {
max = tmp->data;
to_move = tmp;
}
tmp = tmp->next;
}
tmp = head;
if(to_move == head) //if it's already in head position
return sort_ll(head->next);
while(tmp->next != to_move) //find it
tmp = tmp->next;
tmp->next = tmp->next->next; //move it to head
to_move->next = head;
head = to_move;
return sort_ll(head->next); //call function again
}
}
My code doesn't "remember" old values, it just prints out minimum, because that is whats left in the end.
How can I fix this please?